Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
This question was closed Jan 24, 2014 at 03:04 PM by DeadKenny for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by DeadKenny · Jan 24, 2014 at 11:40 AM · gameobjectinstantiatetransformarrayarrays

Instantiate from array into array?

Ok I have two arrays one with gameobjects and another with positions.

What I need is for the code to pick a gameobject at random and instantiate it at the positions once per position.

The bottom line is that I understand how to instantiate a gameobject into an array of positions but don't know how to instantiate an array of gameobjects into an array of positions. Randomly picking from gameobject array would be best too... and it has to be once per position not all into each position in the array of positions.

Here is the code(pseudo) so far: public GameObject[] models; public Transform[] instPos; //position transforms.

 //This is the part I am lost at.
 
 for(int i = 0; i < instPos.Length; i++){
 
  // Instantiate(from models list? into instPos[i].... and so on...
 
 //I'm confused help me please. Thanks.
 
 
 }
 
 
 

My game work in progress: http://forum.unity3d.com/threads/200629-Omega-Void-WIP

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

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by ArkaneX · Jan 24, 2014 at 12:27 PM

Maybe not a super optimal, but quick solution. Requires System.Linq namespace.

 var tmpList = models.ToList();
 for(int i = 0; i < instPos.Length; i++)
 {
     var randomIndex = Random.Range(0, tmpList.Count);
     var instance = (GameObject)GameObject.Instantiate(tmpList[randomIndex]);
     instPos[i] = instance.transform;
     tmpList.RemoveAt(randomIndex);
 }

Untested, but even if there's any typo, general idea remains.

EDIT: I read the question again, and it seems I misunderstood it and you don't want to instantiate into the second array, but use positions from second array. In this case code would be:

 var tmpList = models.ToList();
 for(int i = 0; i < instPos.Length; i++)
 {
     var randomIndex = Random.Range(0, tmpList.Count);
     var instance = (GameObject)GameObject.Instantiate(tmpList[randomIndex], instPos[i].position, instPos[i].rotation);
     tmpList.RemoveAt(randomIndex);
 }
Comment
Add comment · Show 7 · 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 NickP_2 · Jan 24, 2014 at 12:29 PM 0
Share

Correct me if I'm wrong, but the random.Range's max value should be the arraylength - 1?

avatar image ArkaneX · Jan 24, 2014 at 12:31 PM 1
Share

You're wrong :) I was just about to comment your answer. $$anonymous$$ax value parameter in Random.Range for floats is inclusive, but for integers it is exclusive.

avatar image NickP_2 · Jan 24, 2014 at 12:39 PM 0
Share

Aha, never too late to learn something new, thanks :)

avatar image ArkaneX · Jan 24, 2014 at 12:45 PM 0
Share

NP. Don't forget to correct your answer.

avatar image NickP_2 · Jan 24, 2014 at 12:50 PM 0
Share

I used ints, so its exclusive as you said? :)

Show more comments
avatar image
1

Answer by NickP_2 · Jan 24, 2014 at 12:26 PM

  public void SetPosition(GameObject[] models, Transform[] instPos)
     {
         ArrayList idsTaken = new ArrayList();
         int id;
         for (int i = 0; i < models.Length; i++)
         {
             id = Random.Range(0, models.Length - 1);
             while (idsTaken.Contains(id))
             {
                 id = Random.Range(0, models.Length - 1);
             }
             idsTaken.Add(id);
             GameObject clone = Instantiate(models[id], instPos[i].position, transform.rotation) as GameObject;
         }
     }

I hope this is what you were searching for! Might be catchy with the while loop, but this should spawn 10 items, each at a position randomly chosen from an array filled with positions. Be sure to make the array of models the same length as the intPos length or an index error will occur!

GL!

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

20 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

Related Questions

Instantiating from an object that's in an array 1 Answer

Instantiate Game Object on Array of Transforms 1 Answer

Cannot find the length of an array 3 Answers

Keep adding targets to a list 2 Answers

Rotating a Vector3 in Instantiation 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