Quantcast
Channel: Questions in topic: "error"
Viewing all articles
Browse latest Browse all 7934

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

$
0
0
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!

Viewing all articles
Browse latest Browse all 7934

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>