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 /
avatar image
0
Question by Omer.Hussain · Apr 24, 2014 at 12:23 PM · instantiatearraylist

How do we instantiate random sprites from an array...

Hi ... i am working on 2d game in which i have flower which is made with 2d petals(sprites)....totals petals are 30... what i am trying to do when my scenes start it randomly pick 20 to 30 petals and instantiate them on their actual position....i know i can do that with array or list .. i have searched and tried but its complicated... below is my solution but its not good.. first it instantiate random petals but they are not on there actual position .. they overlap eachother second it also instantiate the same petals again ... in array we can remove the element which already been instantiated so it do not copy again ... but when i try it show me error ... its an essay task but i don't know its complicated for me .. :( ..

 #pragma strict
 
 var petals : GameObject[ ];
 var random: int;
 //var location:Vector3= new Vector3(0.9006966,0.5747727,-4);
 
 function Start(){
     
     var random = Mathf.Abs(Random.Range(1,30));
     Debug.Log(random);
     for (var i:int =1; i<=random; i++){
         var petal= petals[Random.Range(0,petals.Length)];
 //        if (petal==petals[Petal1])
 //        {
 ////        location:transform=
 //        }
         var petalClone= Instantiate(petal,transform.position,transform.rotation);
 //        Destroy(petals[i]);
 //        petals.RemoveAt(i);
     }
 }
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

Answer by robertbu · Apr 24, 2014 at 03:00 PM

I'm a bit fuzzy on the rotation part of the problem, but I'm going to take a shot at an answer that will get you at least part of the way there. First, rather than try and remove things from the array, just shuffle the array. You do that by walking through the array and randomly swapping elements. As for rotation, assuming the original game objects have the correct rotation, you can use the single parameter version of Instantiate() and then position the petals.

 #pragma strict
  
 var petals : GameObject[ ];
 var random: int;
  
 function Start(){

     // Shuffle
     for (var k = 0; k < petals.Length; k++) {
         var j = Random.Range(0, petals.Length);
         var go = petals[k];
         petals[k] = petals[j];
         petals[j] = go;
     }

     var amount = Random.Range(20,30);
      for (var i = 0; i < amount; i++) {
        var petalClone = Instantiate(petals[i]);
        petalClone.transform.position = transform.position;
     }
 }
Comment
Add comment · Show 2 · 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 Omer.Hussain · Apr 25, 2014 at 06:54 AM 0
Share

@robertbu Thanks for the answer... its good but the number of petals won't change and the shuffling part do not work as well in my case because it will change the shape of the flower as well ... what i am trying to do is instantiate the petals on their actual position but just every time when user start the scene change the number of petals.... number of petals is working in my script but there position and instantiate same petal is not .. what i though , when my loop runs, in first loop, pick one petal from the array, instantiate it and remove it from the array so in the second loop it won't repeat.... i tried to use Destroy and RemoveAt it should have worked but may be my bad luck :( .. in my game i am restricted to have different number of petals every time ... ;)

avatar image robertbu · Apr 25, 2014 at 02:58 PM 0
Share

I'm having trouble understanding why shuffling change the shape, and line 16 changes the amount in the range of 20 to 30. But you know what you want. You cannot remove entries from a built-in array. What you have to do is use a .NET list. This is essentially what you describe:

 #pragma strict
 
 import System.Collections.Generic;
 
 var petals : GameObject[ ];
 var random: int;
  
 function Start(){
  
       var petalList : List.<GameObject> = new List.<GameObject>(petals);
  
     var amount = Random.Range(1,30);
     for (var i = 0; i < amount; i++) {
        var j = Random.Range(0, petalList.Count);
        var petalClone = Instantiate(petalList[j]);
        petalClone.transform.position = transform.position;
        petalList.RemoveAt(j);
     }
 }  

But this code will also shuffle the positions. I'm guessing the heart of your issue is that you want all the petals to be instantiated in the same order they appear in the array...just have some of the petals missing. Here is yet another way to approach the problem. What it does it remove (by marking them null) petals from the original list to get down to a specific count. Then it instantiates any non-null entries.

 #pragma strict
 
 var petals : GameObject[ ];
 var random: int;
 var $$anonymous$$Petals = 5;
 var maxPetals = 20;
  
 function Start(){
  
     var wanted = Random.Range($$anonymous$$Petals, maxPetals);
     var discard = petals.Length - wanted;
     
     if (wanted >= petals.Length) {
         Debug.Log("maxPetals must be less than the count of petals!");
         return;
     }
     
     while (discard > 0) {
         var i = Random.Range(0, petals.Length);
         if (petals[i] != null) {
             petals[i] = null;
             discard--;
         }
     }
  
     for (var j = 0; j < petals.Length; j++) {
         if (petals[j] != null) {
                var petalClone = Instantiate(petals[j]);
                     petalClone.transform.position = transform.position;
            }
     }
 } 

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.

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

A node in a childnode? 1 Answer

Button showing when array is full 1 Answer

How can I instantiate parented ui objects above previously instantiated children? 1 Answer

Can´t instantiate objects in list correctly 1 Answer

instantiate problem help? 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