I am creating a GameObject out of an Array in Start. The thing is that i only know the Arrayname plus position and that the GameObject variable is called spawnBG. So GameObject.Find
wont work, as much as i know. The Destroy function just gives me the Error (Can`t use variable before it is declared).
`using UnityEngine;
using System.Collections;
public class BG_with_Switch : MonoBehaviour {
public static int levelNow = 0;
public GameObject[] backGrounds;
private int levelOld;
public static int levelMax;
// Use this for initialization
void Start () {
Debug.Log("Array" + (backGrounds.Length - 1));
levelOld = levelNow;
levelMax = backGrounds.Length - 1;
GameObject spawnBG = (GameObject)Instantiate(backGrounds[levelNow]);
}
// Update is called once per frame
void Update () {
if (levelOld != levelNow)
{
Destroy(spawnBG);
GameObject spawnBG = Instantiate(backGrounds[levelNow]);
levelOld = levelNow;
}
}
}`
The best case i could think of would be, if I could Destroy the the old Object by its Arrayname. Like: backGrounds[1]
etc.