To pass data between scripts C#

Hi everyone,

I already read many articles about how to pass data between script in Unity, but neither of them work for me. This is how i am trying:

  1. I have two objects: a cube (named Cube1) and a sphere (named Sphere).

  2. Both have Rigidbody.

  3. In Cube1 script I create the script “cubeCollision”. There is a public variable called “contactPointPaddle = 10”.

  4. In Sphere script I create the script “BallBehavior”. I am trying to acess the value of “contactPointPaddle = 10” this way:
    padContactX = GameObject.Find(“Cube1”).GetComponent().contactPointPaddle;

But nothing happen! Even I set “contactPointPaddle = 10” as static and try to acess by padContactX = cubeCollision.contactPointPaddle nothing happen as well.

What I have missed?

PS: some links I read:

PPS: sorry for my poor english. Still studing…

    private int contactPointPaddle;
    private cubeCollision padContactX;

    void Awake()
    { 
        padContactX = GameObject.Find("Cube1").GetComponent<cubeCollision>();
    }

    void Start()
    {
        contactPointPaddle = padContactX.contactPointPaddle;
    }
1 Like

I not knowed “Awake”, but it works for me. Thank you!

Thread closed.