- Home /
Strange Problem: NullReferenceException
Here is what it says
"NullReferenceException: Object reference not set to an instance of an object at ItemCounter.Update () [0x0001c] in C:\Users\Documents\3dPlatformer\Assets\C# Scripts\ItemCounter.cs:16 (Filename: C Line: 0)"
NullReferenceException: Object reference not set to an instance of an object
I know what this means, I have the game objects set in the inspector.
This error only occurs in the Built Standalone version.
There is no error when in the Unity3d Editor Player.
If anyone can help with this that would be amazing! Thanks
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ItemCounter : MonoBehaviour {
private bool finished = false;
public int maxItems;
public GameObject enableThisObject;
public GameObject disableThisObject;
public int currentItems = 0;
void Update(){
if(currentItems >= maxItems && finished == false){
enableThisObject.SetActive(true);
disableThisObject.SetActive(false);
finished = true;
}
}
void OnTriggerEnter(Collider items){
if(items.gameObject.name == "item"){
currentItems += 1;
}
}
void OnTriggerExit(Collider leave){
if (leave.gameObject.name == "item") {
currentItems -= 1;
}
}
}
which line exactly is line 16? In your posted code line 16 is void Update()
. I don't think it references this line.
Yes, I saved, I restarted and all that stuff but unfortunately no luck.
Answer by naveen_pambi · Jan 27, 2014 at 12:47 PM
if(currentItems >= maxItems && finished == false){
***if(enableThisObject != null && enableThisObject != null)***
{
enableThisObject.SetActive(true);
enableThisObject.SetActive(false);
finished = true;
}
}
try with adding one more condition for checking null. like above
But that is not solving the problem. It is just not doing, what it should do. If he assigned the gameobjects in the Inspector, then they should not be null, but exactly that is happening.
Error is NullReferenceException means some of the gameobjects is null. $$anonymous$$ay be he assigning the gameobjects in the Inspector after Update is called. So, above condition helps in the same.
I know, what NullReferenceExceptions means, but he is not able to assign the objects in the Inspector while on standalone. I understood, that he assigned the gameobjects, then tried it in the editor (and it worked) and then he built it and it did'nt work in standalone. So a) he used something, like Application.IsEditor, or b) he did something different from what he described or c) The engine has a bug. I personally think it is b.
Answer by Quonce · Dec 27, 2019 at 01:58 PM
I think I found a solution - do not use "incremental GC"
Your answer
Follow this Question
Related Questions
RaycastHit2D not interacting with tags 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers