• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by count zero · Apr 24, 2010 at 12:47 AM · instantiateprefabarraysspawning

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Doc

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image count zero · Apr 24, 2010 at 01:58 PM 0
Share

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.

avatar image spinaljack · Apr 24, 2010 at 03:15 PM 0
Share

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

avatar image count zero · Apr 24, 2010 at 09:23 PM 0
Share

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!

avatar image spinaljack · Apr 24, 2010 at 10:18 PM 0
Share

No problem :)

Your answer

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

How Do I Add An Instantiated Object To An Array? 3 Answers

How to Make an Expanding Spawn Area for Prefabs? 2 Answers

Prefabs instantiated from an array are keeping their public int value 1 Answer

Having multiple objects fire prefabs in different times C# 0 Answers

Instantiate random object from Array 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges