array of gameobject?

hi i want to create a array of game object but idk how
GameObject[ ] Bloques;
Bloques = new GameObject[6];

(are 6 object)

That’s correct for creating the array, but each element of the array is null until you add/create a gameobject to put in there. :slight_smile:

how can i addd each gameobjecvt to each array?

Use Lists if you don’t absolutely need an array:

using System.Collections.Generic; //or whatever that is

List<GameObject> GameObject_List = new List<GameObject>();

This is a great way of avoiding using GameObject.Find() within Update() or after your game has started.

how put all the gameobject on list ?

You gotta do a foreach loop, hang on let me get the code snippet…

only found this, xmlreader.Bloques.transform.localPosition = new Vector3(6, 8, 9); i think is more easy the array right?, anyway in array i think u can put the 6 gameobject on public and put it on unity

It’s

foreach (GameObject gameObject in GameObject.FindObjectsOfType(typeof(GameObject))) {
            MySceneGameObjectListOrWhatever.Add (gameObject);
}

Note: Make sure this runs at the very beginning of your game.

Inside of your scripts you can then access this list instead of using GameObject.Find().

Create a method after this called:

public GameObject GetGameObjectFromMyList(string name) {
  foreach(GameObject gameObject in MySceneGameObjectListOrWhatever) { 
     if (gameObject.name == name) { 
        return gameObject;
     }
  }
  //run error game object not found code here
}

Those names are horrible but yeah…

The problem with using arrays if that they’re not dynamic. I use arrays at the moment for only one thing, which is storing simple 2d tile data, and even for that you can use a 2 dimensional list instead.

Try to make use of Lists instead, you won’t regret it.

i have this switch

switch (x)
{
case 1:

break;
case 2:

break;
case 3:

break;
case 4:

break;
case 5:

break;
case 6:

break;

}

but now x is not more a int is a gameobject some solution ? like Random xList = new Random(); should work ?

Attach this script to a main game empty object:

class MyMainGameScript : MonoBehaviour {

   using System.Collections;
   using System.Collections.Generic;

   public List<GameObject> Bloques = new List<GameObject>();

   Awake() {
     //Probably a good idea to run an IEnumerator and yield until
     //all game objects are added to list, but for now I'll just place this here:
      foreach (GameObject gameObject in GameObject.FindObjectsOfType(typeof(GameObject))) {
         Bloques.Add (gameObject);
       }
   }

   public GameObject GetGameObject_FromBloques(string name) {
       foreach(GameObject gameObject in Bloques) {
           if (gameObject.name == name)
           {
           return gameObject;
           }
       }
    Debug.LogError("Game Object not found in Bloques!");
    return null;
    }
}

Here’s a second script can access that GetGameObject_FromBloques method:

class ObjectOneScriptExample : MonoBehaviour {
  using System.Collections;
  using System.Collections.Generic;

  private MyMainGameScript myMainGameScript;
  private GameObject myFirstBloqueObject;

  //Start is just an example:
  Awake() {
     //Alternatively to this, you can make the main game
     //script static so you don't have to use GameObject.Find here...
     myMainGameScript = GameObject.Find("MyMainGameObject").GetComponent<MyMainGameScript>();
 
     //Then we can use it:
     myFirstBloqueObject = myMainGameScript.GetGameObject_FromBloques("My First Bloque"); //if the game object i named My First Bloque
  }

 Update() {
 
  //Now we can do stuff to myFirstBloqueObject here...

  }

}

That’s untested but it’s one way of doing it.

After this, go into script execution order and make sure that the main game script is executed first. That’ll of course get bloated so maybe consider having a first script that is the ultimate first script run for your game no matter what that would add to all game object lists.

this should work ?
using UnityEngine;

public class test : MonoBehaviour {

public List Bloques = new List();

void Start()
{
switch (Bloques)
{
case “base”:

break;
case “1”:

break;
case “2”:

break;
case “3”:

break;
case “4”:

break;
case “5”:

break;
case “6”:

break;
case “7”:

break;
case “8”:

break;
}

void Awake()
{
//Probably a good idea to run an IEnumerator and yield until
//all game objects are added to list, but for now I’ll just place this here:
foreach (GameObject gameObject in GameObject.FindObjectsOfType(typeof(GameObject)))
{
Bloques.Add(gameObject);
}
}

}

public GameObject GetGameObject_FromBloques(string name)
{
foreach (GameObject gameObject in Bloques)
{
if (gameObject.name == name)
{
return gameObject;
}
}
Debug.LogError(“Game Object not found in Bloques!”);
return null;
}
}

Assets/test.cs(42,9): error CS1547: Keyword `void’ cannot be used in this context
lol ?