Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by SirBoboHobo · Mar 30, 2015 at 07:46 AM · c#uinullreferenceexceptiondistance

NullReferenceException: Object reference not set to an instance of an object

i dont understand why im getting this problem, here's my script public class Pause : MonoBehaviour {

     public Text label;
     public DistanceSystem anotherScript;
         // Use this for initialization
     void Start () {
 
     }
     void Awake(){
         anotherScript = GetComponent<DistanceSystem> ();
     }
     // Update is called once per frame
     void Update () {
 
         label.text = anotherScript.distanceTravelled.ToString ("F0");
     }

i dragged the text that the distancesystem is attached to it as the public distancesystem anotherscript, and label is the text i want to display the score at distance system looks like this

     public Transform player;
     public  Text label;
     public float distanceTravelled; 
     float lastPosition;
 
 
 
     void Start () {
 
         lastPosition = player.transform.position.y;
 
     }
     
     // Update is called once per frame
     void Update () {
         distanceTravelled = player.transform.position.y - lastPosition;
         label.text = distanceTravelled.ToString ("F0")+"m";
 
         }         

which works fine, just when i press pause it doesn't print it where i want it and returns the error in the title

Comment
Add comment · Show 2
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 Hrungdak · Mar 30, 2015 at 08:02 AM 0
Share

You can insert a Debug.Log into the Awake() method:

  void Awake(){
     anotherScript = GetComponent<DistanceSystem> ();
     if (anothierScript == null)
         Debug.Log("Awake: Distancesystem not found.");
     else
         Debug.Log("Awake: Distancesystem found.");
 }
 

Same goes to Update:

 void Update () {
     if (anothierScript == null)
         Debug.Log("Update: Distancesystem not found.");
     else
         Debug.Log("Update: Distancesystem found.");
     
     label.text = anotherScript.distanceTravelled.ToString ("F0");
 }


This shows you if the Distancesystem was found in Awake and is initialized in Update. You can see the result in the Console window (Ctrl-Shift+C).

I think of two things that can go wrong:

  • your pause script is attached to a game object that does not contain the DistanceSystem as a component.

  • maybe there is a problem with the initialization order of some classes. Try to move the code from Awake to Start.

avatar image Graham-Dunnett ♦♦ · Mar 30, 2015 at 09:47 AM 0
Share

Reading questions tagged with NullReferenceException is always a good plan. I'd have closed this as a duplicate if your question didn't already have an answer.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by andyblem · Mar 30, 2015 at 09:46 AM

i will assume your null error exception points you at the code inside your Pause Update function. That error means you are trying to access an object which is not there, which means the anotherScript variable is null. Which leads us back to your awake function. This implies that the initilization statement in the Awake function does not work(it is not capturing anything). You can post the code when you press the pause button to see what you are trying to do

Comment
Add comment · Show 3 · 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
avatar image SirBoboHobo · Mar 30, 2015 at 10:22 AM 0
Share

im using the OnClick() of the new UI

avatar image meat5000 ♦ · Mar 30, 2015 at 10:56 AM 0
Share

Try the code in Start rather than Awake. The object might not be available at that moment.

avatar image Nido · Mar 30, 2015 at 11:09 AM 0
Share

And be sure the object is enabled when the scene starts

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

22 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Get Distance From Mouse To Canvas BoxCollider 1 Answer

[C#] Can't get variable from other script. Yields NullReferenceException 1 Answer

Adding and removing specific components to a list based OnTriggerEnter/Exit, Getting Null Exceptions 1 Answer


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