random occuring NullReferenceException
I have the following problem: My game throws me follwing error at a random occuring basis. I have not found any pattern in it or anything. When I start the game and try to interact with an object sometimes the error pops up. If not the game runs smooth till the finish. I'm quite helpless with it. Has anyone an Idea?
NullReferenceException
at (wrapper managed-to-native) UnityEngine.Behaviour:get_isActiveAndEnabled ()
at UnityEngine.EventSystems.UIBehaviour.IsActive () [0x00002] in C:\buildslave\unity\build\Extensions\guisystem\UnityEngine.UI\EventSystem\UIBehaviour.cs:22
at UnityEngine.UI.Graphic.SetVerticesDirty () [0x00002] in C:\buildslave\unity\build\Extensions\guisystem\UnityEngine.UI\UI\Core\Graphic.cs:100
at UnityEngine.UI.Text.set_text (System.String value) [0x00053] in C:\buildslave\unity\build\Extensions\guisystem\UnityEngine.UI\UI\Core\Text.cs:138
at Score.AddToScore1 (Int32 i, System.String name) [0x000ff] in D:\Unity\TauchroboterSpiel_25_04\Assets\Scripts\Score.cs:43
at Collectable.OnEndDrag (UnityEngine.EventSystems.PointerEventData eventData) [0x00188] in D:\Unity\TauchroboterSpiel_25_04\Assets\Scripts\Collectable.cs:166
at UnityEngine.EventSystems.ExecuteEvents.Execute (IEndDragHandler handler, UnityEngine.EventSystems.BaseEventData eventData) [0x00008] in C:\buildslave\unity\build\Extensions\guisystem\UnityEngine.UI\EventSystem\ExecuteEvents.cs:80
at UnityEngine.EventSystems.ExecuteEvents.Execute[IEndDragHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) [0x00073] in C:\buildslave\unity\build\Extensions\guisystem\UnityEngine.UI\EventSystem\ExecuteEvents.cs:269
UnityEngine.DebugLogHandler:Internal_LogException(Exception, Object)
UnityEngine.DebugLogHandler:LogException(Exception, Object)
UnityEngine.Logger:LogException(Exception, Object)
UnityEngine.Debug:LogException(Exception)
UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1) (at C:\buildslave\unity\build\Extensions\guisystem\UnityEngine.UI\EventSystem\ExecuteEvents.cs:273)
UnityEngine.EventSystems.StandaloneInputModule:ProcessTouchPress(PointerEventData, Boolean, Boolean) (at C:\buildslave\unity\build\Extensions\guisystem\UnityEngine.UI\EventSystem\InputModules\StandaloneInputModule.cs:302)
UnityEngine.EventSystems.StandaloneInputModule:ProcessTouchEvents() (at C:\buildslave\unity\build\Extensions\guisystem\UnityEngine.UI\EventSystem\InputModules\StandaloneInputModule.cs:200)
UnityEngine.EventSystems.StandaloneInputModule:Process() (at C:\buildslave\unity\build\Extensions\guisystem\UnityEngine.UI\EventSystem\InputModules\StandaloneInputModule.cs:183)
UnityEngine.EventSystems.EventSystem:Update() (at C:\buildslave\unity\build\Extensions\guisystem\UnityEngine.UI\EventSystem\EventSystem.cs:287)
Here the related part of the Code (it's a MonoBehaviour):
public int score1 = 0;
public int score2 = 0;
public Text txtscore1, txtscore2;
List<string> collected1 = new List<string>();
List<string> collected2 = new List<string>();
public OnScreenText onScreenText;
void Start(){
onScreenText = GameObject.Find("GlobalController").GetComponent<OnScreenText>();
}
void Awake() {
DontDestroyOnLoad(transform.gameObject);
}
public void AddToScore1(int i, string name){
if(collected1.Count > 0){
bool add = true;
foreach(string s in collected1){
if(s == name){
add = false;
}
}
if(add){
collected1.Add(name);
score1 += i;
txtscore1.text = "" + score1;
onScreenText.DisplayText("+"+i,1f,1);
}
}else{
collected1.Add(name);
score1 += i;
txtscore1.text = "" + score1;
onScreenText.DisplayText("+"+i,1f,1);
}
}
@$$anonymous$$D$$anonymous$$Lab, did you succeed to solve it? If yes, can you share?
Answer by Kaasa_Abdul · Jan 19, 2018 at 05:10 PM
Perhaps you forgot to unsubscribe the handler when the object gets destroyed.
I wrote
private void Awake() {
Event += Handler;
}
but forgot
private void OnDestroy() {
Event -= Handler;
}
and thus had the same error.
Your answer
Follow this Question
Related Questions
Why isnt the enabled property part of an interface? 0 Answers
Scoring Points with UI Problem! 0 Answers
Score, FixedUpdate and Update 0 Answers
How do I make score appear when game is over and record high score? 2 Answers
How to make a score counter? 0 Answers