Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MerlinsMaster · Dec 11, 2015 at 01:53 PM · setactivegame object

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MerlinsMaster · Dec 11, 2015 at 03:41 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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

How to stop a game object from going into it's original active state from editor mode.,Make a game object remain deactivated 1 Answer

Saving an object from script to project? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges