- Home /
SetActive - setactive(true) for one object, setactive(false) to the rest
Hey i have a GameObject array full of cameras. These are linked to different enums which are operated with a switch state.
switch (state)
{
case State.Player1:
SwitchCameras();
cameras[1].SetActive(true);
break;
case State.Player2:
SwitchCameras();
cameras[2].SetActive(true);
break;
case State.none:
SwitchCameras();
cameras[3].SetActive(true);
break;
}
What i want to do is when one of the camera = true, then the other cameras = false. And this i want to control with the SwitchCameras function:
void SwitchCameras(){
for (int i = 0; i < cameras.Length; i++){
//stuff
}
}
The foor loop will check through all cameras. The question here is how it can see a gameobject that is active, and then know to disable the others. And then when a before false camera becomes true the others become false again.
Thanks
Answer by Soos621 · May 31, 2016 at 06:31 PM
What you want to do is when one of the cameras is activated you will want to pass a reference of that camera component then check the array for all cameras that are not the same so something like
private Camera[] allCams;
void SwitchCamera (Camera activatedCam){
for(int i = 0; i < allCams.length; i++){
if(!allCams[i].Equals(activatedCam)){
allCams[i].enabled = false;
} else {
allCams[i].enabled = true;
}
}
}
Hey thanks a lot it worked :) i edited it abit so that it would use gameobject array and not camera array. Because cameras uses audio listener and its best to deactivate cameras so there is always only one listener that is active.
Here it is:
void SwitchCameras(GameObject activatedCamera){
for (int i = 0; i < cameras.Length; i++){
if(!cameras[i].Equals(activatedCamera)){
cameras[i].SetActive(false);
}
else{
cameras[i].SetActive(true);
}
}
}
No problem if you ever get a chance look up lists ins$$anonymous$$d of arrays they are a bit easier to deal witg
Your answer
Follow this Question
Related Questions
Set Bool in Inspector to true or False 2 Answers
Problems with save 1 Answer
What's wrong code if-else with tag script ? 0 Answers
Array SetActive C# Unity 1 Answer
I can't figure out how to enable / disable a trigger. 2 Answers