- Home /
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!
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.
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. :)
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)
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.
The problem was that some of game objects had two similar (same) script (Chop). I deleted the double-scripts and it is now working!
But your explain was really useful. I'll use it in the future thank you. :)
Your answer
![](https://koobas.hobune.stream/wayback/20220611234704im_/https://answers.unity.com/themes/thub/images/avi.jpg)