- Home /
Question by
Tarik_m · Jan 07, 2016 at 10:03 PM ·
nullreferenceexceptionlightclass
Null reference exception after instantiating a light gameobject
Hi,
I coded a class, where I declare a Light as an attribute. In the constructor I instantiate the Light object before using it, but I got the null reference exception.
using UnityEngine;
using System.Collections;
public class Node{
public bool walkable;
public Vector3 worldPosition;
public bool Selected;
public Light NodeLight;
public Node(bool _walkable, Vector3 _worldPos) {
Selected = false;
walkable = _walkable;
worldPosition = _worldPos;
NodeLight = new Light();
NodeLight.type = LightType.Spot;
NodeLight.transform.position = new Vector3(worldPosition.x, worldPosition.y + 3f, worldPosition.z);
NodeLight.enabled = false;
}
}
Thank you for your help
Comment
Check your warnings, Light is a UnityEngine object and should not be created using new Light().