- Home /
SOLVED : Adding and retrieving instantiated objects from Array
I've looked through many posts found on forum and here and still have not been able to get anything functional.
I have objects that are being instantiated from functions that are called elsewhere in the script. There are 4 different prefab shapes that are instantiated from a switch/case in this spawn function. At the same time, I have another function that runs that spawns container prefabs that match whatever prefab shape is spawned from that other function (confusing eh?).
The main problem is that when the spawnShape function runs it spawns the corresponding container right away. I don't want the container prefab changing right away. Currently, the moment the shape is spawned, the container spawns to latest shape. What I've been attempting is it to cache/store the spawns of the container prefabs in an array as to not switch/spawn the container to the last shape spawned, but spawn incrementally to those before the latest. I have been unsuccessful storing the instantiated objects in an array. I've attempted the many examples of storing gameobjects in arrays that I have come across... but alas, no dice.
It's either hard to explain, or I'm doing a horrible job of explaining it. My apologies if that's the case. Any input is greatly appreciated.
Example (hopefully easy to follow):
containerArray = Array();
function Start(){
containerArray.length = 10;
}
function spawnShapes(){
var randomPrefab : int = Random.Range(1,5);
switch (randomPrefab) {
case 1: prefabToSpawn = shape1Prefab; fillContainerArray(container1Prefab); break;
case 2: prefabToSpawn = shape2Prefab; fillContainerArray(container2Prefab); break;
case 3: prefabToSpawn = shape3Prefab; fillContainerArray(container3Prefab); break;
case 4: prefabToSpawn = shape4Prefab; fillContainerArray(container4Prefab); break;
}
var randomRotation : int = (360 - (Random.Range(1,8) * 45));
var spawnNum : int = Random.Range(1,7);
var spawnPoint = GameObject.Find("SpawnPoint"+ spawnNum);
Instantiate (prefabToSpawn, spawnPoint.transform.position, Quaternion.Euler(0,0,randomRotation));
}
function spawnContainers(){
var oldContainer = GameObject.FindWithTag("container");
if(oldContainer != null) {
Destroy(oldContainer);
Instantiate(containerArray[0], oldContainer.transform.position, Quaternion.Euler(0,0,0));
shiftArray();
}else{
Instantiate(containerArray[0], Vector3(0,0,10), Quaternion.Euler(0,0,0));
shiftArray();
}
}
function fillContainerArray( x : Transform){
for(var i=0; i<containerArray.length; i++){
if(containerArray[i] == null){
containerArray[i] = x;
return;
}
}
}
function shiftArray(){
for(var i=0; i<9; i++){
containerArray[i] = containerArray[i+1];
}
}
UPDATE: This is the functional code now working.
Answer by spinaljack · Apr 24, 2010 at 08:31 AM
Are you using unity arrays or javascript arrays? Unity arrays are faster but are fixed while javascript arrays has all the standard array functions like push and shift.
I was trying to use the javascript arrays so I could use .Shift. I'll adjust my code above to include the last array I attempted. I honestly may be going about this completely wrong.
Have you tried dumping the contents of your array in a debug log to see what's actually going on in there?
I did as you suggested with the debug.log and it was not acting as it should. Thanks for the suggestion! I took note of it and fixed a couple things. Also made a function to use rather than directly use .Shift. Thanks again!
Your answer
Follow this Question
Related Questions
Instantiate random object from Array 2 Answers
How to spawn a new prefab when the last one was destroyed?? 2 Answers
How do you spawn multiple prefabs within a certain distance from each other from one spawn point? 2 Answers
Making a reset function for arrayed objects 2 Answers
prevent object from falling to the left side when instantiated (spawned) ? 1 Answer