GetComponent null, but it's attached to gameobject
Hey everybody, so I wrote a script, which creates two objects. In one of these object scripts, it want to get the objects SpriteRenderer component and it's attached to! But everytime I run the game it says NullReferenceException. The line with the error is a comparison between the spriterenderer color of the objects, so I check first if one of them is null.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
public class LockDoor : MonoBehaviour {
public List<Color> colGen = new List<Color>();
public int colNow;
public int i;
public Transform doorsGO;
public Animation lockAnim;
public Controls controls;
public ScoreManagement scoreScript;
private new SpriteRenderer renderer;
void Awake()
{
// Find GameObejcts
doorsGO = GameObject.FindWithTag("doors").transform;
scoreScript = GameObject.Find("Score").GetComponent<ScoreManagement>();
//doorsGameObject = GameObject.Find("doors");
controls = doorsGO.GetComponent<Controls>();
}
// Use this for initialization
void Start () {
// GetComponents
lockAnim = GetComponent<Animation>();
lockAnim["LockTheDoor"].speed = 2.0f;
// Liste mit Farben
colGen.Add(Color.blue);
colGen.Add(Color.green);
colGen.Add(Color.yellow);
colGen.Add(Color.red);
// Change Color für Schlüssel
renderer = GetComponent<SpriteRenderer>();
colNow = Random.Range(0, colGen.Count);
renderer.color = colGen[colNow];
// Change Color für Türen
foreach (Transform colChi in doorsGO)
{
SpriteRenderer col = colChi.GetComponent<SpriteRenderer>();
i = Random.Range(0, colGen.Count);
col.color = colGen[i];
colGen.RemoveAt(i);
}
}
IEnumerator WaitForAnimation () {
lockAnim.Play();
controls.enabled = false;
scoreScript.enabled = true;
print("You've locked the door!");
yield return new WaitForSeconds(lockAnim.clip.length);
}
void OnTriggerEnter2D(Collider2D other)
{
SpriteRenderer othCol;
othCol = other.gameObject.GetComponent<SpriteRenderer>();
if (renderer != null)
{
if (renderer.color == othCol.color) // here is the line with the error
{
StartCoroutine(WaitForAnimation());
}
else
{
print("Wrong door!");
}
}
}
}
Thanks and sorry for my bad english!
The gameObject referenced to get the SpriteRenderer component doesn't have one directly attached to the gameObject. Is the collider pointing to a top level object in the hierarchy(look at the panel for the object hierarchy). You may want to try using GetComponentInChildren since it will transverse the object graph looking for SpriteRenderer types, this is more expensive of course, but it seems like you're assu$$anonymous$$g the SpriteRenderer exists at a particular position in the gameobject hierarchy.
Thanks for the comment, but I don't know what you really mean. The object that colliding with this object, is a child, but I thought that this isn't important, because the Collider just look for the other object(in OnTriggerEnter), and not whether it's a child of an other object. But I tried it out, and the error is still there :/
Answer by IgorAherne · Mar 12, 2017 at 06:54 PM
It's been ages and not really related to a question, but the post is a perfect opportunity to explain an issue I was having.
I was instantiating a prefab from within a Coroutine, and calling GetComponent immediately afterwards. Turns out, I had to yield return null
before attempting to getComponent inside this corutine, to give unity a chance to set-up the components.
That's true because when breakpointing, the Start() method in the instantiated prefab wasn't hit until the next frame.