- Home /
NullReferenceException when setting Component as Variable, but no bugs
This is the code I'm currently using for a flag collectible in a platformer. The stage scrolls automatically, and each time the flag collectible leaves view, it's put into the object pool (controlled by different script). When it's reenabled, it's given a new random color. The color decides which animation is used, and changes the Start Color of the particle system. The Flag is an empty game object with an Animator, a Particle System, and this script.
using UnityEngine;
using System.Collections;
public class Flag : MonoBehaviour {
private Animator animator;
private Renderer renderer;
private ParticleSystem particle;
public int maxcolor = 4;
private Color myyellow = new Color(1f, 0.8f, 0f);
private Color mygreen = new Color(0.45f, 0.8f, 0.3f);
private Color myblue = new Color(0.1f, 0.65f, 0.87f);
private Color myred = new Color(0.9f, 0.4f, 0.1f);
public int flagColor;
void Start()
{
renderer = transform.Find("flagSprite").GetComponent<Renderer>();
animator = transform.Find("flagSprite").GetComponent<Animator>();
particle = transform.Find("burst").GetComponent<ParticleSystem>();
SetColor();
}
void OnEnable()
{
SetColor();
}
void OnDisable()
{
renderer.enabled = true;
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
renderer.enabled = false;
particle.Play();
}
}
void SetColor()
{
flagColor = Random.Range(0, maxcolor);
animator.SetInteger("flagColor", flagColor);
switch(flagColor)
{
case 0: //yellow
particle.startColor = myyellow;
break;
case 1: //green
particle.startColor = mygreen;
break;
case 2: //blue
particle.startColor = myblue;
break;
case 3: //red
particle.startColor = myred;
break;
}
}
}
Everything works as intended. There's no gameplay issue or noticeable bug, but I'm getting a NullReferenceException the first time renderer, particle, or animator is used. I can get rid of this error by replacing
animator.SetInteger("flagColor", flagColor);
with
transform.Find("flagSprite").GetComponent<Animator>().SetInteger("flagColor", flagColor);
but I shouldn't have to do that EVERY TIME, right? That's the point of storing the component in a variable.
EDIT: I get the NullReferenceException with the variables renderer, animator, and particle unless I replace all uses with with the full GetComponent.
Answer by Baalzamon · Feb 19, 2015 at 02:03 PM
Hi,
I had a similiar problem with assigning a component in the Start() method, but it is a component of the same GameObject.
Anyhow.
game = GetComponent()
would result in the error you described, whereas
game = GameObject.Find("Game").GetComponent();
throws no NullPointerReference Exception. I'm not sure if that's helpful for you. :)
I think it's a little odd, that you have to use the static Find()
method to retrieve the gameobject, which should be stored in the gameObject
variable anyways. Using game = gameObject.GetComponent()
throws the same error though.
Your answer
Follow this Question
Related Questions
NullReferenceException-Error when trying to GetComponent 3 Answers
Why is this null? Finding a script on an object 3 Answers
NullReferenceException. GetComponent dose not work properly 2 Answers
NullRef on my parent script 1 Answer
GetComponent help 2 Answers