A question about Instanciating and GameObject indexing

So lets start with GameObject Indexing. I would like to have an array of prefab GameObjects that will be stored as an array so that a script can call an object to instanciate by an ID. The only thing is that I need the numbering to be controllable and consistent.

My other question is more simple, I have a script that will be attached to a gameObject that is going to be spawning the prefabs. The script attached to the prefab needs to read a variable from the GameObject whose script created an instance of it.

Arrays indexes are consistent… they’re numbered in order 0->N. And they’re controllable in that you order the objects in the array however you want.

So… you must mean something else by controllable and consistent that I’m not inferring from your post. Can you explain more in depth what you mean? And don’t describe it in the context of arrays… instead describe the problem you’re attempting to solve, for which you thought arrays were a possible answer.

So this sounds like a spawner script. In which case I usually have a ‘OnSpawned’ message (usually implemented as a interface). You can include a parameter that is the Spawner that spawned the object. When the spawner spawns the object, it calls this message on the spawned object.

public interface ISpawnable
{
    void OnSpawned(Spawner spawner);
}

With that reference it can access what it needs.

With the Array solution I have a group of GameObject prefabs for different projectiles. The objects store all their data and properties. With the current system I have the way that projectiles are spawned is by the GameObject that is going to create them having ProjectileID variable. I was hoping to have a bunch of projectiles that setting the ID would allow it to spawn the projectile matching that ID.

The other part, yes it is a spawner script. Its actually for the same projectile system. The projectile’s scripting will change their properties slightly based on what object spawned the projectile. I need their scripts to be able to read the “InternalName” variable that the spawner has.

I was able to get the spawner working but I still need a good way to organize and store prefabs so that they can be loaded from a numerical ID.