Gameobject is null for no reason
So here is my problem:
I have a Gameobject with a script attached. Some other script calles this script.
Now here is the deal:
Sometimes the object and thus also the script are null. This wouldnt be so strange if i couldnt see gameobject and script in the hirarchy while a debug tells me that they are null...
I made a debug that debugs the object every frame and... weird thing... Its null every second frame, but present in the frames between...
Really dont know whats going on here...
Anyone got any help?
Can you post some code? The only thing i can think of right now is that something else is destroying it on the 2nd frame
if you don't call the script in a proper way you will get null it have nothing to do with game object if it active in hirarchy or not so show us your code how do you call this script
So here is my code:
using UnityEngine;
using System.Collections;
public class CardLine : $$anonymous$$onoBehaviour
{
private SpriteRenderer myRenderer;
void Awake ()
{
myRenderer = GetComponent<SpriteRenderer> ();
}
public void initialize ()
{
name = transform.parent.name + "_line";
deactivate ();
}
public void activate (Color color)
{
myRenderer.enabled = true;
myRenderer.color = color;
}
public void deactivate ()
{
if (this) {
myRenderer = GetComponent<SpriteRenderer> ();
if (myRenderer.enabled) {
myRenderer.color = Color.white;
myRenderer.enabled = false;
}
}
}
}
I use this script to outline cards in a card game. Before I activate the card I deactivate all of them --> deactivate() gets called. I know this works, as a debug in this method gets activated. But when I try to turn off the spriterenderer I get the message that it has been destroyed. So I added an if to check if it was there. But now I got an error that the object is destroyed. So I added an if to check if "this" is present but as I said its null on every seconds frame :(
Pls help :)
so provide the error log & the other script you use to call this script functions & to which objects thees scripts is attached to i am afraid you try disable SpriteRenderer of thees objects which they might not have .
So i dont want to spam you with screenshots here...
Just trust me that the object has a spriterenderer
Why do you need the script that calls this method? Its not important....
The error is: $$anonymous$$issingReferenceException: The object of type 'SpriteRenderer' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object.
Please note that this error sometimes doesnt say that my spriterenderer is null but that the whole gameobject its attached to is destroyed.
Ali hatem
Cant add more comments.
So if i got this correctly you think that the script is trying to disable the spriterender of the object that calls deactivate(). I checked that.
So I think you are wrong here. I placed a Debug.Log(gameobject.name) in deactivate() and it debugs the correct name of the object that has the sprite renderer.
you are rigte i tested it my self sorry abot that.
Ali hatem
No problem. I have done some more testing. The error now appears on the line of the debug. So it debug fine for most objects but there is always one or mabye two (or mabye more... it seams random) gameobject that cant even debug themselves because they have been destroyed.
But I am now sure that no gameobject is actually destroyed.
What i have done is:
void Update ()
{
Debug.Log (FindObjectsOfType<CardLine> ().Length);
}
to check if any objects actually get destroyed. But the number remains constant, even after the error. Also I have now stopped enabeling and disabeling the renderer. Ins$$anonymous$$d I just set it to an invisible color. But I still get the error...
I am at the end here xD
Answer by ComradeVanti · Mar 28, 2016 at 09:25 PM
Hey guys. So I have figured out the problem... So I will just answer my onw question here xD
So: Problem was that something with my Lists wrent wrong. You know I have a List of CardLines and I deactivated them... so for some reason each CardLine existed 2 times but the second clone or what it was, was some kind of virtual element that didnt acctually exist... so that was the problem :)