- Home /
How to switch game objects on and off?
Hey guys,
I have three game objects (p1, p2, p3) parented under one game object (Switch). I have a C# script attached to the parent game object that is supposed to turn the three child game objects on or off using bools, but I can't get it to work for some reason.
Here is my script:
using UnityEngine;
using System.Collections;
public class switchManager : MonoBehaviour
{
public bool option1; //
public bool option2; //
public bool option3; //
public GameObject choice1; //
public GameObject choice2; //
public GameObject choice3; //
void Start ()
{
choice1 = FindObjectOfType<GameObject>(); // gets access to the p1 game object
choice2 = FindObjectOfType<GameObject>(); // gets access to the p2 game object
choice3 = FindObjectOfType<GameObject>(); // gets access to the p3 game object
if (option1 = true)
{
choice1.gameObject.SetActive(true);
choice2.gameObject.SetActive(false);
choice3.gameObject.SetActive(false);
option2 = false;
option3 = false;
}
if (option2 = true)
{
choice1.gameObject.SetActive(false);
choice2.gameObject.SetActive(true);
choice3.gameObject.SetActive(false);
option1 = false;
option3 = false;
}
if (option3 = true)
{
choice1.gameObject.SetActive(false);
choice2.gameObject.SetActive(false);
choice3.gameObject.SetActive(true);
option1 = false;
option2 = false;
}
}
void Update ()
{
if (option1 = true)
{
choice1.gameObject.SetActive(true);
choice2.gameObject.SetActive(false);
choice3.gameObject.SetActive(false);
option2 = false;
option3 = false;
}
if (option2 = true)
{
choice1.gameObject.SetActive(false);
choice2.gameObject.SetActive(true);
choice3.gameObject.SetActive(false);
option1 = false;
option3 = false;
}
if (option3 = true)
{
choice1.gameObject.SetActive(false);
choice2.gameObject.SetActive(false);
choice3.gameObject.SetActive(true);
option1 = false;
option2 = false;
}
}
}
Could someone tell me what it is that I'm doing incorrectly?
Thanks.
Answer by Bunny83 · Dec 11, 2015 at 02:14 PM
Well, you have several errors here. Some logical and some syntactical errors. First you use the assignment operator (=) in your three if conditions instead of the comparison operator (==). This will turn each option true every time and each if is executed every time. So only the last if statement will take effect. Also FindObjectOfType will always return the same object. As you might know:
It's insane doing the same thing over and over again and expecting different results.
Furthermore even when you would use (==) in the 3 if statements it's pointless to set option1 to false in the second and third if block since that's not possible. If option1 is true the first if block will execute and therefore option2 and option 3 will be set to false. So you can only enter the second or third if when option1 is false. Same for the second and third if block. You can only enter the third if block if option1 and option2 are false.
Using boolean values that way doesn't make much sense. It's far easier to use an array and a single int variable to specify which object should be enabled.
public GameObject[] objects;
public int activeIndex = 0;
public void SetActiveObject(int aIndex)
{
activeIndex = aIndex;
for(int i = 0; i < objects.Length; i++)
objects[i].SetActive(i == activeIndex);
}
void Update()
{
SetActiveObject(activeIndex);
}
This does work with unlimited objects. You also might want to remove the Update method and only call SetActiveObject manually whenever you want to change the active object. Just pass the index of the object you want to activate. All other objects in the array will be deactivated. If you pass an invalid index (smaller than "0" or greater than "Length - 1") all objects will be disabled.
Thanks. You solution works great. I do have a couple of questions, though.
First, is it okay to have the public void before the update? I would have thought that would upset the order of execution, but it worked, so apparently not.
Also, can I use this method to activate any of them at any time, as opposed to just being able to active one at a time? For my purposes, it would be useful to be able to do both of those things.
Thanks again.
Your answer
Follow this Question
Related Questions
physics.OverlapSphere colliders 1 Answer
How to reference game objects across scenes? 1 Answer
Set Objects Child to Active/Inactive(Solved) 5 Answers
Saving an object from script to project? 0 Answers