Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by RDog123 · Apr 25, 2016 at 06:36 PM · unity 5

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

  2. Create the script.

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

  3. Create a text variable of Type Text.

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

  4. Add value to game object.

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

  5. 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.

Comment
Add comment · Show 12
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RakshithAnand · Apr 25, 2016 at 07:37 PM 0
Share

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

avatar image RDog123 RakshithAnand · Apr 25, 2016 at 08:19 PM 0
Share

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.

avatar image RDog123 RakshithAnand · Apr 25, 2016 at 08:30 PM 0
Share

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.

avatar image RakshithAnand RDog123 · Apr 25, 2016 at 09:43 PM 0
Share

Ah i c

And please share a complete screenshot of your gameobject, and inspector

Show more comments
avatar image jamiejammas · Apr 25, 2016 at 09:35 PM 0
Share

could you screenshot your script, hierarchy and inspector window?

avatar image RDog123 jamiejammas · Apr 25, 2016 at 10:28 PM 0
Share

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!

avatar image RakshithAnand RDog123 · Apr 25, 2016 at 10:42 PM 0
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

82 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How much i need pay for the license, but i don't want sell any develop? 1 Answer

Unity Games Looking "Cheesy" 1 Answer

Autocomplete does not work in MonoDevelop 3 Answers

Rotating multiple images continuously (2D) - Mobile performance/optimizations 2 Answers

Unity3d Character Spawn Point 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges