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 sajithsathes · Apr 19, 2019 at 02:36 PM · gameobjectssetactivefor-looptoggle button

toggle gameObjects with a keyPress

So i have four public gameObjects, say, ghost1, ...2, ...3, ...4 and a private gameObject, say, tempGO in my script that is attached to an empty that is the parent of 4 objects that are the candidates for the above said public gameObjects. So when the game starts, ghost1 must be active while the others are not. In the Update method, i am using a for loop to set the next gameObject that is line to be active while the others go inactive.

public class playerSwitching : MonoBehaviour {

 private string characterName;
 private GameObject tempGO;

 //public GameObject ghost1;
 //public GameObject ghost2;
 //public GameObject ghost3;
 //public GameObject ghost4;

 public int characterNumber = 1;

 // Use this for initialization
 void Start () {
     for (int i = 0; i < 5; i++)
     {
         if (i == 1)
         {
             /*characterName = "ghost" + i;
             gameObjRend = GameObject.Find(characterName);
             gameObjRend.SetActive(true);*/
         }
         else
         {
             /*characterName = "ghost" + i;
             gameObjRend = GameObject.Find(characterName);
             gameObjRend.SetActive(false);*/
         }
     }
 }
 
 // Update is called once per frame
 void Update () {

     if (Input.GetKeyDown(KeyCode.Space))
     {
         if (characterNumber < 4)
         {
             characterNumber++;
         }
         else
         {
             characterNumber = 1;
         }

         for(int i = 0; i < 5; i++)
         {
             if (i != characterNumber)
             {
                 /*characterName = "ghost" + i;
                 gameObjRend = GameObject.Find(characterName);
                 gameObjRend.SetActive(false);*/
             }
             else
             {
                 /*characterName = "ghost" + characterNumber;
                 gameObjRend = GameObject.Find(characterName);
                 gameObjRend.SetActive(true);*/
             }
         }
     }
 }

}

The technique I've implemented here doesn't use the public gameObjects. The gameObject that comes in line after the current and active one should be activated while the others are deactivated; in the case where the 4th one is active, the first should be activated. And I read somewhere that Find method is not advised and is also very slow. So can anyone help me here?

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
0

Answer by DiegoSLTS · Apr 19, 2019 at 04:32 PM

You souldn't call Find in an Update method, that is what people usually mean when saying you should avoid using Find. It's OK to use Find every now and then, using it on Start or Awake methods is not bad.

Instead of using 4 different GameObject members I'd use one GameObject[], it's easier to work with it within for loops.

This should be OK for your use case:

 public GameObject[] ghosts;
 public int characterIndex = 0;

  void Start () {
      ghosts = new GameObject[4];
      for (int i = 0; i < 4; i++) {
          ghosts[i] = GameObject.Find("ghost" + (i+1));
          ghosts[i].SetActive(i == 0);
      }
  }
  
  void Update () {
      if (Input.GetKeyDown(KeyCode.Space)) {
          if (characterIndex < 3) {
              characterIndex++;
          } else {
              characterIndex = 0;
          }

          for(int i = 0; i < 4; i++) {
              ghosts[i].SetActive(i == characterIndex);
          }
      }
  }

Important: note the for loop goes from 0 to 3 (i < 4). Arrays are zero-indexed so the first element is 0, not 1. Considering this, it's better to keep track of the index (0 through 3) instead of the ghost number (1 through 4) so the code is cleaner. Since the ghosts names are not zero-based, the Find line adds 1 to the i value so for the first element (i == 0) the first ghost ("ghost1") is found.

Also, if tempGO is the parent of the ghost objects, you could replace GameObject.Find (which searchs in the whole hierarchy) with tempGO.transform.Find (which searches only under the tempGO object) and you'll have less expensive Find calls. And if tempGO only has the ghost objects and you make sure they're in the right order, you can avoid the Find completelly and replace the Find line with something like:

 ghosts[i] = tempGO.transform.GetChild(i);

Note that in this case you don't use the names for anything.

Comment
Add comment · 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

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

107 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

gameObject.SetActive (true); Not working 10 Answers

physics.OverlapSphere colliders 1 Answer

SetActive - setactive(true) for one object, setactive(false) to the rest 1 Answer

Best way to store a new variable in each iteration of a for loop? 1 Answer

Set Bool in Inspector to true or False 2 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