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 Array

Discussion in 'Editor & General Support' started by Faestus, Jul 26, 2014.

  1. Faestus

    Faestus

    Joined:
    Jul 23, 2014
    Posts:
    68
    Which is the right formual to make a GameObject Array?

    I tried:
    var GAME:GameObject[] = new GameObject()[];
    var GAME:GameObject[] = new GameObject();
    var GAME:GameObject[] = new GameObject[]();
    var GAME:GameObject[] = new GameObject[];

    Neither work...

    Additionally, how do I refference an object in my Array by it's name?

    ex. GAME["name"]=new GameObject();
     
    Last edited: Jul 26, 2014
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    8,937
  3. Faestus

    Faestus

    Joined:
    Jul 23, 2014
    Posts:
    68
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    If you just want to define the variable then all you use is

    Code (csharp):
    1. var game : GameObject[];
    If you mean that the size of the array varies at runtime, then don't use an array (which can't be resized), use a generic List.

    --Eric
     
  5. Faestus

    Faestus

    Joined:
    Jul 23, 2014
    Posts:
    68
    Thank you.

    Can i reference objects in a list as GAME["name"]?
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    No, arrays only use numbers to reference items in the array. If you want to use some other kind of reference then you can use a Dictionary.

    --Eric