Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 RadinQue · Aug 06, 2015 at 05:05 PM · errornullreferenceexceptionobject reference

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

Hey! I searched in this forum the solving but it seems I'm a unique case. I wrote a script and I want to attach this script to more than one game object. Specifically I have a Chop script and I attached it to each 'Tree' game objects. It working really well but I get an error: NullReferenceException: Object reference not set to an instance of an object. I don't know why I get this error because the script works well.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class Chop : MonoBehaviour {
 
     public Slider remainSlider;
     public Text resourceText;
     public Text remainingText;
     public GameObject canv;
 
     public float timeBetweenChops = 0.5f;
     public float maxRes;
 
     float memRes;
     float timer;
     bool canChop;
 
     void Start()
     {
         memRes = maxRes;
     }
 
     void Update()
     {
         timer += Time.deltaTime;
         if (timer >= timeBetweenChops)
         {
             canChop = true;
         }
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.tag == "Player")
         {
             remainSlider.maxValue = maxRes;
             remainSlider.value = memRes;
             resourceText.text = "Tree";
             remainingText.text = memRes.ToString();
             canv.SetActive(true);
         }
     }
 
     void OnTriggerStay(Collider other)
     {
         if (other.tag == "Player")
         {
             if (Input.GetButton("Fire1"))
             {
                 if (canChop)
                 {
                     DoChop();
                 }               
             }
         }
     }
 
     void OnTriggerExit(Collider other)
     {
         if (other.tag == "Player")
         {
             memRes = remainSlider.value;
             remainSlider.maxValue = 0f;
             remainSlider.value = 0f;
             resourceText.text = "";
             remainingText.text = "";
             canv.SetActive(false);
         }
     }
 
     void DoChop()
     {
         if (remainSlider.value != 0)
         {
             timer = 0f;
             canChop = false;
             remainSlider.value--;
             remainingText.text = remainSlider.value.ToString();
         }
     }
 }

The error: NullReferenceException: Object reference not set to an instance of an object Chop.DoChop () (at Assets/Scripts/Chop.cs:74) Chop.OnTriggerStay (UnityEngine.Collider other) (at Assets/Scripts/Chop.cs:53)

This is my first game project what I not do based on the tutorials, so it is possible that the code is bad. Please help!

Comment
Add comment · Show 4
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 RadinQue · Aug 06, 2015 at 04:31 PM 0
Share

Yes, I assigned the sliders to those gameobject. Interesting that there are some 'Tree' game object with no errors. So if I collide them I don't get this error message.

avatar image RLin · Aug 06, 2015 at 05:06 PM 0
Share

What line is the issue on?

avatar image RadinQue · Aug 06, 2015 at 05:11 PM 0
Share

I don't really understand this question. But I solved the problem. Thank you Hellium! The problem was that some of the 'Tree' game objects had two same scripts by accident. Thank you again. :)

avatar image Hellium · Aug 06, 2015 at 05:13 PM 0
Share

Add a Debug.Log(name + " - " + (remainSlider.name ?? "No slider assigned")) ; in the DoChop function to make sure you have assigned the slider in every object

@RLin : NullReferenceException: Object reference not set to an instance of an object Chop.DoChop () (at Assets/Scripts/Chop.cs:74) Chop.OnTriggerStay (UnityEngine.Collider other) (at Assets/Scripts/Chop.cs:53)

1 Reply

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

Answer by Graham-Dunnett · Aug 06, 2015 at 05:12 PM

I guess you just don't understand what NullReferenceException means. So, if a variable isn't set to an object, then you need to do a simple piece of code:

 void DoChop()
  {
     if (remainSlider) {
       if (remainSlider.value != 0)
       {
           timer = 0f;
           canChop = false;
           remainSlider.value--;
           remainingText.text = remainSlider.value.ToString();
       }
     } else {
         Debug.Log("something has gone wrong");
     }
  }

This will use the debug log to tell you that remainSlider isn't set.

Now, also, you claim that every object has had remainSlider set correctly. So it would be trivial for you to check that this variable is set in the Start() function.

Comment
Add comment · Show 2 · 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 RadinQue · Aug 06, 2015 at 05:25 PM 0
Share

The problem was that some of game objects had two similar (same) script (Chop). I deleted the double-scripts and it is now working!

avatar image RadinQue · Aug 06, 2015 at 05:29 PM 0
Share

But your explain was really useful. I'll use it in the future thank you. :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException... 1 Answer

Program received signal: “SIGABRT” 0 Answers

CAN SOMEONE FINALLY HELP ME! 3 Answers

NullReferenceException on empty project because of ScriptCompilerBase 0 Answers

Vector3.Distance giving me NullReferenceException every time 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