1
\$\begingroup\$

I have 15 buttons on my screen at onClick event am fetching the button and hiding that button using

EventSystem.current.currentSelectedGameObject.SetActive(false); 

now at the end i have to show buttons again so i am using

for (int i = 0; i < 15; i++)
 {
     tag1 = "Button" + (i + 1);
     GameObject.FindGameObjectWithTag(tag1).SetActive(true);
     Debug.Log("done");
 }

Loop is giving error becuase it is not able to find object which has been hidden or setActive(false) from other references unity forum i see same solution but i don't know why it is not working in my case

i know issue is with "FindGameObjectWithTag" function i will have to replace it with some other

any help will be appreciated
Thanks

\$\endgroup\$
1
\$\begingroup\$

You can try this.

// in void Start
objs = GameObject.FindGameObjectsWithTag (tag);

// void Update()
foreach (GameObject objs in obj) {
    obj.SetActive(true);
}
\$\endgroup\$
1
\$\begingroup\$

Find operation are costly operations: try to avoid them.

Make a public List and assign your buttons to that List and then iterate the List.

   public List<GameObject> allButton; // it will show in your inspector assign (Drag and 
                                      // Drop) all your buttons to this list. 

   public void ShowAllButtons(){

      for(int index=0; index < allButton.Count; index++ ){
         allButton..SetActive(true);
      }

   }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.