Hello fellow unity devs, i was coding away at a kind of spawning system but it just doesn't work, and i can't figure out why. It throws a NullReferenceException whenever i call SpawnStump().
Actual error: NullReferenceException
UnityEngine.Component.get_transform () (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/UnityEngineComponent.cs:19)
GameHandler.SpawnStump () (at Assets/Code/GameHandler.cs:31)
GameHandler.Update () (at Assets/Code/GameHandler.cs:20)
public Transform startStump;
public GameObject stumpToSpawn;
public float spawnPosYDiff = 1;
Transform spawnPosTransform;
// Use this for initialization
void Start () {
startStump = GameObject.Find("Stump1").transform;
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown("SpawnStump")) {
Debug.Log("LOG");
SpawnStump();
}
}
void SpawnStump() {
Vector3 SpawnPos;
SpawnPos.x = startStump.position.x;
SpawnPos.y = startStump.position.y + spawnPosYDiff;
SpawnPos.z = startStump.position.z;
spawnPosYDiff += 1;
spawnPosTransform.transform.position = SpawnPos;
stumpToSpawn = Instantiate(stumpToSpawn, spawnPosTransform.transform.position, startStump.transform.rotation) as GameObject;
}
Does anybody know what is going wrong?
EDIT: Figured it out. Turned out spawnPosTransform was empty, so i just took the transform of an empty gameobject and used that. Next to that i moved the code from the Start void to the Awake void. All is fine now!
↧