Search Unity

Accessing all Players convention

Discussion in 'Scripting' started by Noxwill, Jun 20, 2013.

  1. Noxwill

    Noxwill

    Joined:
    Apr 16, 2013
    Posts:
    18
    Is it better to have a GameState that stores all 'Player' tagged objects in a public GameObject[] where other controllers such as the Camera script (where camera can focus on a different player) can access what players are on scene through that GameState controller..

    or...

    Should you have a private GameObject[] in each script that finds all players for themselves?
     
  2. Marble

    Marble

    Joined:
    Aug 29, 2005
    Posts:
    1,268
    I guess it depends on your design. For some purposes, it may be good enough to tag your players, then use GameObject.FindGameObjectsWithTag(playerTag) to identify them. In some other cases, you may want your players to register and deregister themselves with a single manager object that, like you say, keeps an array (or a List) of them for handy reference.
     
  3. Noxwill

    Noxwill

    Joined:
    Apr 16, 2013
    Posts:
    18
    Right, because I do have a lot of interactions with player selection and HUD changes depending on player selected.

    So I'll keep it in a single manager and other controllers will just take the reference to players from there.