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 glasstongue · May 16, 2014 at 01:27 PM · arraysclonesunique

How to make sure unique, randomly spawned, instantiated prefabs only spawn once

I have a game with hundreds of unique prefab items that need to be randomly generated into the scene. The problem I'm running into is that I can't seem to figure out how to prevent my spawner from creating duplicates from the array I've generated.

I wrote a bunch of if/else statements trying to compare my random array value but I ran into a chicken and the egg problem where I couldn't compare a the array's int because it hadn't been instantiated yet and I couldn't compare the int because it had already been instantiated.

I also tried to compare names and destroy, and search for the "Clone" tag and destroy (both seem round about - any guidance would be great :)

 void Start()
     {
         
         myObjects = Resources.LoadAll<GameObject>("Prefabs");
     
 
         startPosition = transform.position;
 
     }
 
     void SpawnRandomObject() 
     {    
         //spawns item in array between position number 0 thru 49
         int whichItem = Random.Range (0, 49);
     
         //replace gameObject set with script that gathers prefabs from folder
         GameObject myObj = Instantiate (myObjects [whichItem]) as GameObject;
 
         myObj.transform.position = transform.position;
     }


     //my efforts were something similar to this
     void Awake()
 {
     var clones:GameObject = GameObject.FindWithTag("Clone"));

     if(clones)
     {
     Destroy(clones.GameObject);
     }
 }

Thanks! :)

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 Graham-Dunnett · May 16, 2014 at 01:31 PM

Have two lists, A and B. A contains all the unchosen objects, so starts off like your myObjects array and B starts empty. Pick an object from list A, and move it to list B. Over time A will get smaller, and B will get bigger. When you're done with an object, move it back from B into A. (Or, if you never want the object used again, into a new list C which stores the used objects.)

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 glasstongue · May 31, 2014 at 07:26 AM 0
Share

Thanks, it took me forever to figure this out but it ended up helping me a lot.

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

21 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

Related Questions

generating unique names for prefabs within an array 2 Answers

Instantiate random object from Array 2 Answers

Any way to enabled all GameObjects in an Array List? (UnityScript) 1 Answer

How can I access collider's game objects? 1 Answer

Why can't I pass an array reference to another array variable? 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