Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

GameObject manager help

Discussion in 'Scripting' started by will_brett, Jul 6, 2014.

  1. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    Hi everyone, This is quite a vague question but I'm not sure how I should approach this problem.

    I want to create a game object manager that has a list of game objects. I want to use the manager to enable and disable gameobject.Only one object can be active at any time and more game objects can be added to the list.

    example of use would be showing a UI panel, changing a game character etc.

    so far I have been doing something like

    currentgameobject.setactive(true)
    othergameobject.setactive(false)

    but the issue with this is that it is inefficient and time consuming plus it's not exactly reusable.
    Any ideas/suggestion would be greatly appreciated.

    Thanks
     
  2. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Code (csharp):
    1. public List<GameObject> gos = new List<GameObject>();
    2.  
    3. public void Activate(GameObject active)
    4. {
    5.     foreach (GameObject go in gos)
    6.     {
    7.         if (go == active)
    8.             go.SetActive(true);
    9.         else
    10.             go.SetActive(false);
    11.     }
    12. }
    Something like that?
     
    IvanAuda likes this.
  3. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    I think that may be part way there.

    Say I have a selection of buttons and each button turned on one object and turned all the others off so that only one object was on screen at a time.

    How would I access a particular gameobject in the list to turn it on? Would the button have to pass in the gameobject?

    Sorry Im not a coder but trying very hard to learn
     
  4. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,716
    Yes, that's pretty much the idea.
     
    IvanAuda likes this.
  5. will_brett

    will_brett

    Joined:
    Feb 16, 2013
    Posts:
    208
    This worked perfectly. Thank you