So I’m currently trying to instantiate a game object in a scene every few seconds, and that object is constantly changing. I tried linking it as a public variable to the script, but I keep getting null reference exceptions. any idea why this code is unable to spawn the GameObject wall?
using UnityEngine;
using System.Collections;
public class BPMController : MonoBehaviour {
public int bpm;
public GameObject wall;
public GameObject floor;
bool wait = false;
int spawnCounter = 0;
public int spawnInterval = 4;
//find the wall in the scene
void Start () {
wall = GameObject.FindGameObjectWithTag("wall");
}
void Update () {
//if coroutine isnt running start it
if (!wait)
{
wait = true;
StartCoroutine(waitCoRoutine());
}
//if interval count is reached, spawn wall
if(spawnCounter >= spawnInterval)
{
spawnWall();
spawnCounter = 0;
}
}
//coroutine to wait a set time and increment counter
IEnumerator waitCoRoutine(){
yield return new WaitForSeconds(bpm/60);
spawnCounter++;
wait = false;
}
void spawnWall(){
//create cloneObject to manipulate
GameObject wallClone = null;
//instantiate wallCone as a clone of wall
wallClone = Instantiate (wall, wall.transform.position, wallClone.transform.rotation) as GameObject;
//set wallcone as a child of floor;
wallClone.transform.parent = floor.transform;
}
}