How can I reference to a component of another gameobject?

Hi,

I watched a unity video tutorials on “GetComponent”. The tutor assigns two scripts to an object in the scene.
the first script reads a variable from the second one and prints the value in the console.

I got how it works. But I don’t get how can I read and probably edit components from another game object.
Let’s assume that I have a sphere and a cube. I assign the first script to the sphere and I want that script shows a value from the second script that has been given to the cube.

But it doesn’t work! This is the error that I get:
NullReferenceException: Object reference not set to an instance of an object
script1.Awake () (at Assets/script/GetComponent/script1.cs:11)

Help is truly appreciated.

first script given to the sphere:

public class script1 : MonoBehaviour {

    public script2 s2;

    // Use this for initialization
    void Awake()
    {
        s2 = GameObject.Find ("cube").GetComponent<script2> ();
    }


    void Start ()
    {
        Debug.Log ("player score is:" + s2.playerScore);
    }
  

    // Update is called once per frame
    void Update ()
    {
  
    }
}

second script has been given to the cube:

    public int playerScore = 10;

    // Use this for initialization
    void Start () {
  
    }
  
    // Update is called once per frame
    void Update () {
  
    }
}
3 Likes

if the cube is put in the game after the object with script one then the s2 will be null… in the Awake do a if (s2 == null) Debug.log(“s2 is null”); that might be the issue

1 Like

Several things:

  1. Make sure you actually have an object called "cube:. Find is case sensitive. I prefer using tags by the way.
  2. Do not use GetComponent in the Find, but rather check if the cube actually has been found:
CubeScript cube_script;

GameObject cube = GameObject.FindWithTag("Cube");

if(cube != null)
{
      cube_script = cube.GetComponent<CubeScript>();
}
  1. You might want to make score a private and use a Get function, but I guess that is just a matter f preference.
public int GetScore()
{
      return playerScore;
}
6 Likes

@MattB79

PERFECT, THANK YOU.

Yes I put the cube after the sphere. So I deleted both objects. Put another cube first and then the sphere. And it is working.

But I don’t get it. Is the order of the objects important?? I totally didn’t know about that. Then creating a game object before another one will be totally different from creating after it? And how can I see and check the order of the objects in the scene?

@Fluzing

Yes, I made the mistake calling “Cube” with lower case in my code.

Edit → project settings → script execution order. This might work also instead of deleting game objects. And yes its always good to check for nulls like Fluzing posted when using find methods

Thank you so much…you made my day and saved my life also…:slight_smile:

This link helped me. https://gamedev.stackexchange.com/questions/92654/unity-prefab-script-gameobjects-cant-be-ones-on-the-scene/92657#92657?newreg=fd64c4ad2cc244048e46b68f8865bf82

Shouldn’t your “s2=” be in Start?
First all objects are Awakened
Then they are Started
So when Start is called, you should be able to find all objects, no matter what order they are in.

To say it in more detail, when the game starts Unity creates all the gameObjects for the scene. Awake() runs instantly as each one is made: create A, Awake on A, create B, Awake on B … … Next, after everything is created, Unity goes back and runs Start() on everything. Both times to set-up an object make sense, so Unity gave us both, and a choice to use either or both (or none).

It won;t matter which one you use unless you want to talk about another gameObject. That’s why new Unity scripts have Start() and not Awake(). If you use myCatRB=myCat.GetComponenet(); in Start(), we know your cat object has been created along with it’s rigidbody. We don’'t know if your cat has run it’s Start() yet, but that won’t matter.

In rare cases you need both. Suppose your cat has to create it’s rigidbody. That’s weird but could happen. So the intermediate-level advice is: use Awake() to set-up yourself. It happens before anyone will ever see you. Use Start() to do stuff using other objects. They will be safe since they did all of their required stuff in their Awake(). It’s a little complicated, but not because of Unity. Starting-up any computer thinig is a little complicated.