Object Reference Explanation

Hello!

Recently, I started taking an online course from UDemy called “Learn to Code by Making Games - The Complete Unity Developer”.

The course so far has been pretty explanatory, and if you stick to what they’re doing you will never run into a problem.

However, I guess you could call me a daredevil because I don’t really enjoy following the guidelines.

Obviously, like the noob I am, I ran into a little hiccup part way through my game creation endeavour.

I am not sure how to explain the situation I ran into fully, and if I do not explain enough I will happily attempt to restructure my explanation.

So I have this game, and in the game it has a “Lives Counter” that works off of a “Lives” script, (Just some simple code that I put together)

When we were taught in the course how to change text, the instructor told us to do something along these lines.

(Screenshots aswell to make things more clear)

  1. Create the GameObject (Obviously)

http://pichoster.net/images/2016/04/25/TextExample.png

  1. Create the script.

http://pichoster.net/images/2016/04/25/ScriptExample.png

  1. Create a text variable of Type Text.

http://pichoster.net/images/2016/04/25/Variable.png

  1. Add value to game object.

http://pichoster.net/images/2016/04/25/Value.png

  1. Reference it to the Script and the GameObject.

http://pichoster.net/images/2016/04/25/Reference.png

I will note that the above example was just a little countdown script that used

Time.deltaTime;

to create a countdown effect, so the value was constantly changing.

However, recently when I attempted to the EXACT same thing, the only thing that I changed, was that the value the Text displayed was dependent on the user pressing a button. (Just to make sure the code worked).

However, the error (I guess this is a pretty common one for noobs in unity.)

“object reference is not set to an instance of an object”

would keep popping up.

After a little bit of searching, I found the solution for my problem was this.

text = GameObject.Find("LivesCounter").GetComponent<Text>();

I don’t fully understand what exactly this line of code is doing, and in what scenarios I should use it (If anyone wants to explain that would be awesome)

I am curious why doing what I had done in the past was not acceptable in this case. Cause to my knowledge both things are essentially doing the same thing… I could be completely wrong, again, I’m a total noob.

If anyone could explain to me why I needed to reference the Object in the code and not in the interface by dragging and dropping it to where I attached the script component I would be very grateful.

Also, if someone could explain what the second line of code is doing I would be very grateful.

To answer your question

text = GameObject.Find(“LivesCounter”).GetComponent();

is often used to get a reference to a private object that isnt exposed to the inspector

in the above code you are saying:

the variable “text” is equal to some GameObject named “LivesCounter”'s Text component.

this tells your script that the text variable is LivesCounter’s Text Component and that what ever you do to the text variable should be reflected in the LivesCounter’s Text Component.
this is essentially the same as dragging a public variable (or serialized private variable) into the appropriate slot in the inspector.

to sum up, you would use GameObject.Find so that you can get a reference to an object that isnt already referenced in the inspector.