简体   繁体   中英

How do I deactivate all game objects in an array except ONE?

In my game, I plan to make tutorials appear for my game. As in you press T to make the tutorial window appear and press Q and E to cycle through multiple tutorial images. The tutorial images are game objects. So, if I got 10 tutorial images.. and need only ONE of them active at a time (while the others are deactivated), how do I do it?

I have tried this:

tutorialBubbles[] is the array of game objects.

public GameObject[] tutorialBubbles; //delete the above one if we're going with multiple 
bool tutorialNeeded;

void checkIfTutorialNeeded() {
    
        if (Input.GetKeyDown(KeyCode.T))
        {
            tutorialNeeded = true;
        }

        if(!tutorialNeeded && Input.GetKeyDown(KeyCode.T)){

            tutorialBubbles[0].SetActive(false);
        }

        if (tutorialNeeded)
        {
            tutorialBubbles[0].SetActive(true);
            

            if (Input.GetKeyDown(KeyCode.G))
            {
                tutorialBubbles[1].SetActive(false);

                for (int i = 2; i < tutorialBubbles.Length; i++)
                {
                    tutorialBubbles[i].SetActive(true);   
                }
            }
        }

You can define some integer values for demonstrate which ArrayID wont be inclueded. For example:

public GameObject[] tutorialBubbles; //delete the above one if we're going with multiple 
bool tutorialNeeded;
int exceptArrayId;
if (Input.GetKeyDown(KeyCode.G))
            {
                tutorialBubbles[1].SetActive(false);
                exceptArrayId = 1;

                for (int i = 0; i < tutorialBubbles.Length; i++)
                {
                    if(i == exceptArrayId)
                    {
                        tutorialBubbles[i].SetActive(false);
                    }
                    else
                    {
                        tutorialBubbles[i].SetActive(false);
                    }
                    
                }
            }

I defined a int(exceptArrayId) for this example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM