- Home /
NullReference Exception for instantiate
Im running this code. It outputs 0 (meaning a gameobject called floor does exist)
Then outputs an error:
NullReferenceException: Object reference not set to an instance of an object TerrainGenerator.Start () (at Assets/TerrainGenerator.cs:13)
I have been attempting to get this to work for over an hour now. Could someone explain what is going on?
using UnityEngine;
using System.Collections;
public class TerrainGenerator : MonoBehaviour {
private GameObject cube;
void Start () {
Random.seed = 42;
for (int i = 0; i < 20; i = i + 1){
GameObject cube = Instantiate(transform.Find("dirt"),new Vector3(-30,0, i), Quaternion.identity) as GameObject;
Debug.Log (GameObject.Find("floor").transform.position.z);
cube.transform.parent = GameObject.Find("floor").transform;
}
}
}
Comment