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)
Create the GameObject (Obviously)
Create the script.
Create a text variable of Type Text.
Add value to game object.
Reference it to the Script and the GameObject.
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.
Is this GameObject where you are referencing your Text component, a prefab instance?
And check the "text" variable field at runtime.. is it changing to empty once u start the game?
If its a prefab instance and you have not applied the changes to the prefab instance then it will revert back to the original prefab state, on which the "text" component might be empty
I see what you mean, I never really did check that out while running the debugger. I'll check it out and get back to you!
Thanks for the insight.
So, in response, the variable never changes while running the game. It just stays there while the "NullReferenceException: Object reference not set to an instance of an object" is repeated in the console.
the "text" component has the proper Text GameObject attached to it. I am not really sure why the error pops up.
Here are the screen shots, I hope that I understood what you were referring to, I am still super noob at this.
This is what it looks like when its not running, http://pichoster.net/images/2016/04/25/NotRunning1.png
This is what it looks like when it is running, http://pichoster.net/images/2016/04/25/WhileRunning1.png
Again, I hope this is what you mean when you say that it could potentially be reverting back to the original state.
Ah i c
And please share a complete screenshot of your gameobject, and inspector
could you screenshot your script, hierarchy and inspector window?
Yeah sure, Ill remake it so that I can replicate the GameObjects that create the error.
I will rename them to be LivesCounter2.
(I must note that I have found a solution, I am just trying to fully understand why I had to do it the one way and not the original way)
Here is the code for the script (pretty basic).
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class LivesCounter2 : $$anonymous$$onoBehaviour {
public Text text;
void Start()
{
text.text = ("Lives: " + Lives.playerLives.ToString());
}
void Update()
{
text.text = ("Lives: " + Lives.playerLives.ToString());
Debug.Log(text);
}
}
Screenshots of the,
Hierarchy
http://pichoster.net/images/2016/04/26/Hierarchy.png
Text GameObject Inspector
http://pichoster.net/images/2016/04/26/TextInspector.png
Script GameObject Inspector
http://pichoster.net/images/2016/04/26/ScriptInspector.png
Hope this helps!!
I'm assu$$anonymous$$g the issue is pretty obvious, but then again, I am very new!
Took a look at your images.
From what I see, You have added LivesCounter2 script to both the Text object and the gameobject which is unnecessary.
U just need to reference the Text component of the UI Text object in the normal gameobject and it will update accordingly. No need to add the scripts two times.
Answer by jamiejammas · Apr 25, 2016 at 09:47 PM
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.