- Home /
setactive doesn't appear to work on instanciated prefab gameobject array
I wrote some code for toggling through cameras:
It works but isn't elegant and doesn't allow for adding more cameras easily.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ChooseCamera : MonoBehaviour {
public GameObject Standard_Camera_Prefab;
public GameObject FlyCam;
private GameObject Standard_Camera;
private GameObject Fly_Camera;
private int CameraIndex =0;
// Use this for initialization
void Awake () {
//Instantiate All Cameras
Standard_Camera = Instantiate (Standard_Camera_Prefab) as GameObject;
Standard_Camera.SetActive (true);
Fly_Camera = Instantiate (FlyCam) as GameObject;
Fly_Camera.SetActive (false);
}
}
void Update () {
if (Input.GetButtonUp ("ToggleCameras")) {
if (Standard_Camera.activeSelf){
Standard_Camera.SetActive(false); CameraIndex=1;
}
if (Fly_Camera.activeSelf){
Fly_Camera.SetActive(false); CameraIndex=0;
}
}
// set new camera active
if (CameraIndex == 0)
Standard_Camera.SetActive (true);
if (CameraIndex == 1)
Fly_Camera.SetActive (true);
}
}
So I decided to re-write it as a prefab array of gameobjects containing cameras so I can add as many as I like by expanding the array.
However no matter what I do, the numbers iterate properly which I can observer in the inspector BUT the .SetActive(bool) does nothing!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ChooseCameraFromArray : MonoBehaviour {
public GameObject[] CameraArrayOfPrefabs;
public int cameraIndex;
void Awake () {
//Instantiate All Cameras into the array
foreach (GameObject item in CameraArrayOfPrefabs)
{
GameObject NewCamera = (GameObject)Instantiate(item) as GameObject;
}
foreach (GameObject item in CameraArrayOfPrefabs)
{
item.gameObject.SetActive(false);
}
CameraArrayOfPrefabs [0].gameObject.SetActive(true);
cameraIndex = 0 ;
}
void Update () {
if (Input.GetButtonUp ("ToggleCameras"))
{
CameraArrayOfPrefabs[cameraIndex].SetActive(false);
if (CameraArrayOfPrefabs.Length-1 != cameraIndex)
{
cameraIndex += 1;
}
else
{
cameraIndex = 0;
}
//set camera index to active
CameraArrayOfPrefabs[cameraIndex].SetActive(true);
}
}
}
Is there something I'm missing with prefab gameobjects in arrays ?
Answer by robertbu · Mar 08, 2014 at 10:51 PM
Instantiate() creates a copy or clone of an object. First question: is the CameraArrayOfPrefabs really an array of prefab items? Assuming so, on line 16, you create copies of each camera in the array, but you are not capturing those copies. That is you have the 'originals' or 'blueprints' for your cameras in the CameraArrayOfPrefabs, but they are not the same as the copies you make. You are iterating through the array of prefabs setting the active state of the prefabs, but that does not in any way impact the copies you made through Instantiate(). You need to capture each camera you create in a new array or generic list. It is that new array or list you iterate through when you want to set the active state of those objects.
Answer by zcoldrick · Mar 08, 2014 at 11:37 PM
ok solved it similarly to how you suggested, not sure if having two arrays is the most effective way to deal with it but perhaps it is.
using System.Collections.Generic;
public class ChooseCameraFromArray : MonoBehaviour
{
public PersistentScript thePersistentScript;
public GameObject[] CameraArrayOfPrefabs;
private GameObject[] myCameraArray;
public int cameraIndex;
// Use this for initialization
void Awake ()
{
//Instantiate All Cameras into the array
myCameraArray = new GameObject[CameraArrayOfPrefabs.Length];
for (int i = 0; i < CameraArrayOfPrefabs.Length; i++) {
GameObject myGameObject = new GameObject ();
myGameObject = Instantiate (CameraArrayOfPrefabs [i]) as GameObject;
myCameraArray [i] = myGameObject;
}
foreach (GameObject item in myCameraArray) {
item.SetActive (false);
}
myCameraArray [0].SetActive (true);
cameraIndex = 0;
}
void Update ()
{
if (Input.GetButtonUp ("ToggleCameras")) {
myCameraArray [cameraIndex].SetActive (false);
print ("Camera index set to" + cameraIndex);
if (myCameraArray.Length - 1 != cameraIndex) {
cameraIndex += 1;
} else {
cameraIndex = 0;
}
//set camera index to active
myCameraArray [cameraIndex].SetActive (true);
}
}
}