- Home /
Question by
Liem · Dec 02, 2014 at 08:19 AM ·
errornullreferenceexception
Error in changing color from sendmessage
okay, so i want to make an object alternate between appearing and disappearing, so i made a code like this:
s = GetComponent<SpriteRenderer> ();
void SetVisibility (bool b)
{
if(b)s.color=new Color(1f,1f,1f,1f);
if(!b)s.color=new Color(1f,1f,1f,0f);
}
it works just fine, but when i tried calling from another object like this:
charsprite.SendMessage("SetVisibility",true);
i got an error like this:
NullReferenceException
UnityEngine.SpriteRenderer.set_color (Color value) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/SpritesBindings.cs:199)
can anyone help me?
Comment
Answer by taxvi · Dec 02, 2014 at 09:01 AM
the problem is that the variable 's' is not referring to the 'SpriteRenderer' when you call the function. try moving the first line inside the function:
void SetVisibility (bool b)
{
s = GetComponent<SpriteRenderer> ();
if(b)s.color=new Color(1f,1f,1f,1f);
if(!b)s.color=new Color(1f,1f,1f,0f);
}