2

I have a bool variable (isGrounded) from my player's movement control script that I want to access in another GameObject.

BallController.cs

public class BallController : MonoBehaviour {
    Transform myTrans;
    Rigidbody2D myBody;

    public bool isGrounded = true;
    public bool release = false;
}

GravityPull.cs

public class GravityPull : MonoBehaviour {

    public GameObject target;
    public int moveSpeed;
    public int maxdistance;
    private float distance;


    void Start ()
    {
        target= (GameObject.Find("Ball (1)"));
    }


    void Update ()
    {
        distance = Vector2.Distance (target.transform.position, transform.position);

        if (distance < maxdistance && target.isGrounded)
        {
             target.transform.position = Vector2.MoveTowards(target.transform.position, transform.position, moveSpeed * Time.deltaTime / distance);
        }
    }
}

If I make my target a GameObject than I can find it using .find. But if I do this I can't access the bool. If I make my target a BallController then I can access the bool, but I can't use .find to find the class. I also can't cast the GameObject as a BallController. Can someone tell me what I'm doing wrong here?

1
  • Is your target suppose to be a BallControler ? You could simply call GetComponent Commented Dec 10, 2015 at 16:01

1 Answer 1

5
target.getComponent<BallController>().isGrounded

this should be sufficient.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.