I keep getting the same error on this and it’s had me stumped for hours. Essentially I instantiate an object called “unit”, which has a script called movement which instantiates “head” (in the start function). Except that it doesn’t instantiate it, the following code gives this.
public class Movement : MonoBehaviour { //Movement is the behaviour which allows a unit to move
public Transform HeadPrefab;
public Transform Block;
public int MaxSize = 5;
Transform head;
List<Transform> blockList = new List<Transform>();
void Start () {
Vector3 headPosition = new Vector3(0,5,-11);
head = Instantiate (HeadPrefab, headPosition, Quaternion.identity) as Transform;
Debug.Log(head.transform.position.x);
}
}
gives: NullReferenceException: Object reference not set to an instance of an object.
at runtime, when compiled there are no errors.
The unit object is instantiated by a script called GameArena and is as follows
public Transform unit;
public Transform enemy;
Transform clone;
void Start () {
Vector3 unitPosition = new Vector3(0,5,-11);
Vector3 enemyPosition = new Vector3(10,5,-11);
clone = Instantiate (unit, unitPosition, Quaternion.identity) as Transform;
Instantiate (enemy, enemyPosition, Quaternion.identity);
Debug.Log(clone.transform.position.x);
Debug.Log(clone);
}
again no errors at all on this and it reports to the console the correct position of 0.
I’m confused that I seem to be doing the same things twice but it only works one time, what’s different in “Movement”?