add and remove from array

Hi there guys

Please, PLease I need assistance with this;

I have a loop that instantiate x amount of particles (spheres) in a area. Now, later, I need to be able to access some of the individual spheres by a name or something. There are various reasons, but the main one is to change the color between red and blue for a particular instance. I know I can probably store them in an array, but what should I store? A number, a name or what? I noticed that Unity does not give each ball an individual instance name when it get’s instantiated…

Please assist with this urgent problem, thanks in advance!

anyone?

Your question is a little confusing, which is why it’s gone unanswered. You first talk about particles, then you mention spheres. It’s difficult to tell if you’re using Unity’s particle system or not.

If it’s spheres and not particles, simply store an array of the renderer component of each game object or the game object themselves. If it’s a particle system of flat fake spheres, then you already have direct access to an array of particles structs. It’s all in the documentation.

http://unity3d.com/support/documentation/ScriptReference/ParticleEmitter-particles.html

In the future, try and be more clear when composing a question.

Sorry about the unclear question. I should actually leave out the word “particles”, since it is just normal spheres I’m instantiating. Does this help or change the picture from your answer?

No, I don’t think it changes the answer. As Quiteus said, you’ll want to store references to the actual things you later need to change (or, at least something through which you can access what you need to change). So, that could be an array of Gameobjects, an array of Transforms, an array of Renderes, etc depending on your needs.

Ok, so I have done as you guys commanded, could you maybe help with an example of storing a prefab with reference?

public GameObject newAtom;
public List<GameObject> atomList = new List<GameObject>();

Then, later in the start function I have below, but I’m not sure how can I add this as an unique entity to the array…

int i = 0;
        while (i < maxAtomsA) 
        {
            int rndX = Random.Range(-8, 8);
            int rndY = Random.Range(-8, 8);

            GameObject thisAtom = newAtom;
            Instantiate(thisAtom, new Vector3(rndX, rndY, 2), Quaternion.identity);
            
            i++;
        }

Thanks for the help!

ok, I sorted this by adding

thisAtom.name = “atom” + i.ToString();

right before instantiating the gameobject

This code doesn’t make any sense… You create a List of GameObjects named atomList, but then never use this list…

I think what you are trying to do is instantiate a list of game objects… if this is the case, it’s better to just use an array… something like this:

GameObject[] atomList = new GameObject[MAX_ARRAY_LENGTH];    // MAX_ARRAY_LENGTH is the num of game objects you want to create.

void Start()
{
     for(int i = 0; i < MAX_ARRAY_LENGTH; i++)
     {
          int rndX = Random.Range(-8, 8);
          int rndY = Random.Range(-8, 8);
          
          atomList[i] = (GameObject) Instantiate([refabName, new Vector3(rndX, rndY, 2), Quaternion.identity);
     }
}

Then, to access a particular element in the array, just use “atomList[indexNumer]”.

I hope this answers your question.

EDIT: If the number of game objects you want to instantiate is unknown or always changing, then you need to use a List instead of an array… if this is the case, then things will be done much differently.

Thanks Jon

You are correct in the sence that the number of atoms are unknown. I have addapted my code to this, I hope using an ArrayList is fine - I’m not sure the difference between List and ArrayList:

public GameObject newAtom;
public ArrayList atomList = new ArrayList();

and then in start

int i = 0;
        while (i < maxAtomsA) 
        {
            int rndX = Random.Range(-8, 8);
            int rndY = Random.Range(-8, 8);

            GameObject thisAtom = newAtom;
            thisAtom.name = "atom" + i.ToString();
            Instantiate(thisAtom, new Vector3(rndX, rndY, 2), Quaternion.identity);
            atomList.Add(thisAtom.gameObject);
            i++;
        }

No, no! NEVER use an ArrayList object! It’s VERY inefficient! Instread, you should use a List object like you did originally… here is how you would do that:

Anyway, you say that you wouldn’t know the number of atoms ahead of time, yet you use the maxAtomsA variable in your while loop. If you are using a while loop and looking at the number of instantiations, then you obviosuly know ahead of time the maximum number of atoms.

The code I gave above will work.

and ArrayList is essentially an Object encapsulating an array with methods for adding, removing, etc… the problme is the agortithms for these methods are VERY inefficient and should NEVER be used in games… a List object is really just a Basic Linked List. It works entirely different from an array and has its benefits over arrays, as well as its shortcomings. for what you are doing a simple Array is the best choice (An ARRAY not an ARRAYLIST!).

Try the code above and see how it works. If there are any errors let me know and I will help you resolve them. My method is the most efficient way of doing it.

An ArrayList can hold objects of any type, while a List is type specific. That is, you must tell List what object type it’s going to store (integer, double, gameobject, etc). Since List already knows the data type it’s holding, there’s no need to cast anything when retrieving data. Because of that, code using List is generally cleaner (and likely faster) than code using ArrayList.

If you know the data type going in, you’re probably better off using List instead of ArrayList - though an ArrayList will certainly work.