• 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
0
Question by imagoculturae · Nov 17, 2014 at 10:48 AM · instantiate-objects

Cycle trought array of objects

Hi all, I need a method to cycle trought an array of objects that I instatiate at runtime, at the moment I have this which instatiate only the first element in the array:

 function OnMouseDown(){
     var instance : GameObject = Instantiate(models[0],Vector3.zero,Quaternion.identity) as GameObject;
     }


but how do I make it so that everytime I click a new element is instatiate and the previous one is destroyed?

Sorry I am not a programmer Many thanks in advance!

Comment
Add comment · Show 6
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 Kiwasi · Nov 17, 2014 at 10:50 AM 0
Share

You want for or for each. Check out the learn section.

avatar image imagoculturae · Nov 17, 2014 at 11:29 AM 0
Share

I went a bit trought the for and the foreach loop in the manuals before and I understand how to apply a certain behavior to all the elements at the same time, but I can't really make my head around the concept on how to apply somthing to an element one at the time.

avatar image richyrich · Nov 17, 2014 at 12:08 PM 0
Share

At the very least, you should edit your post with an attempt of for each; then tell us which aspect of the code is problematic.

avatar image Scribe · Nov 17, 2014 at 12:09 PM 0
Share
 var someArray : someClass[] = new someClass[someLength];
 
 for(var i : int = 0; i < someArray.Length; i++){
     DoSomething(someArray[i]); //do something to each element of someArray!
 }
avatar image imagoculturae · Nov 17, 2014 at 12:26 PM 0
Share
 #pragma strict
 var models : GameObject[];
 
 function Start(){
 models = Resources.LoadAll.<GameObject>("Objects");
 
 }
 function On$$anonymous$$ouseDown(){
      var instance : GameObject = Instantiate(models[0],Vector3.zero,Quaternion.identity) as GameObject;
      }
 

This is my full code, the lenght of the array is unknown because I will add the objects into the resource folder as I need them(at the moment thera are only 2 objects, later will be more). I do understand how to instatiate them all at the same time with a for loop but if I put the destroy condition within the for loop then nothing is created also. Logically I can't think how do I instantiate one at the time, so that destroy does not affect all the instances but only the privious

Show more comments

1 Reply

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

Answer by Wisearn · Nov 17, 2014 at 01:07 PM

    #pragma strict
    var models : GameObject[];
    var instance : GameObject;
    var cycleIndex : int;
  
    function Start() {
       models = Resources.LoadAll.<GameObject>("Objects");
    }

    function OnMouseDown() {
       if (instance) {
          Destroy(instance);
       }
       instance = Instantiate(models[cycleIndex],Vector3.zero,Quaternion.identity) as GameObject;
       cycleIndex += 1;
       if (cycleIndex+1 > models.Length) {
          cycleIndex = 0;
       }
    }

I put the "instance" outside of the local scope, so it is retained, then you can simply destroy instance before you make a new one.

And to cycle through the indexes you need to count it yourself and reset it back to 0 when it's needed (as demonstrated with cycleIndex).

We check if cycleIndex+1 is above array.Length because Length returns 5 for 5 items, but array index will count 1 item as index[0], and so on.

Comment
Add comment · Show 1 · 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 imagoculturae · Nov 17, 2014 at 04:34 PM 0
Share

Great this is what I was looking for!!!Did not have a clue that I could use models.lenght with another variable, I have this now so that the first object will be already loaded:

 #pragma strict
          var models : GameObject[];
          var instance : GameObject;
          var cycleIndex : int = 0;
      
          function Start() {
             models = Resources.LoadAll.<GameObject>("Objects");
             instance = Instantiate(models[cycleIndex],Vector3.zero,Quaternion.identity) as GameObject;
          }
       
          function On$$anonymous$$ouseDown() {
             if (instance) {
                Destroy(instance);
             }
             cycleIndex += 1;
             if (cycleIndex+1 > models.Length) {
                cycleIndex = 0;
             }
             instance = Instantiate(models[cycleIndex],Vector3.zero,Quaternion.identity) as GameObject;  
          }



Would be nice to have a option to go the other direction, but I'll work it out on how to do it. Thanks again!!!

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to check if there is space for instantiation ? 0 Answers

Trouble Instantiating C# (error CS1503: Argument `#3' cannot convert `UnityEngine.Quaternion' expression to type `bool') 1 Answer

Instantiated child object incorrect transform 1 Answer

how to instantiate objects for single time 2 Answers

Unity4.6 - Setting text Property on Text Object 1 Answer


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