x


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.

more ▼

asked Apr 24 '10 at 12:47 AM

count zero gravatar image

count zero
13 2 2 6

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

Doc

more ▼

answered Apr 24 '10 at 08:31 AM

spinaljack gravatar image

spinaljack
9.1k 18 31 91

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.

Apr 24 '10 at 01:58 PM count zero

Have you tried dumping the contents of your array in a debug log to see what's actually going on in there?

Apr 24 '10 at 03:15 PM spinaljack

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!

Apr 24 '10 at 09:23 PM count zero

No problem :)

Apr 24 '10 at 10:18 PM spinaljack
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1683
x1260
x356
x110

asked: Apr 24 '10 at 12:47 AM

Seen: 2690 times

Last Updated: Apr 24 '10 at 09:30 PM