HELP! I can’t work out what on earth is going on here! Just so you know, I am trying to spawn two Game Objects randomly at any two of three pre-set spawn locations, while making sure that only one ‘orb’ appears at each.
EDIT: Perhaps I should also note that I am attaching this script to the ‘spawn1’ empty Game Object.
// Currently works, however more than one GameObject often spawns at a given point.
// FIXING
var spawn : Transform[];
var orbs : GameObject;
var orbCount : int = 0;
function Start () {
var randomPick : int = Random.Range(1,3);
if (gameObject.Find('spawn'+randomPick)) {
Debug.Log("WORKS");
}
InvokeRepeating("spawnOrbs", 1, 1);
}
function Update () {
if (orbCount >= 2) {
CancelInvoke();
}
}
function spawnOrbs () {
var randomPick : int = Random.Range(1,2);
Debug.Log(randomPick);
if ("spawn"+[randomPick]) {
var instance = Instantiate(orbs, gameObject.Find('spawn'+randomPick).position, transform.rotation); // Set to instance of object
Debug.Log("Orb Spawned!");
orbCount++;
Destroy(gameObject.Find("spawn"+randomPick)); // Destroys spawnPoint when object spawns!
} else {
Debug.Log("Spawn point doesn't exist");
spawnOrbs();
}
}
It is line “var instance = Instantiate(orbs, gameObject.Find(‘spawn’+randomPick).position, transform.rotation);” that throws up the error. I can’t tell if it has a problem with ‘orbs’ or ‘gameObject.Find(‘spawn’+randomPick)’. I should point out that I have three empty game objects named spawn1, spawn2 and spawn3 in the scene, and have dragged these into the ‘spawn’ variable in the inspector.