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 talyon · Jan 31, 2015 at 12:19 PM · c#instantiatevector3listspawn

Instantiate prefab at certain positions

Hello everyone, I have a small problem with a script, I would like to instantiate objects at certain positions, it all works, but I have the problem that sometimes I instantiates the objects on top of each other.

This is the script that I made

 public List<GameObject> Sprites;

 public int n;

 public List<Vector3> SpawnPosition;



 void Start () 
 {
     SpawnEnemy ();
 }
 
 void Update () 
 {
 
 }

 void SpawnEnemy () 
 {
     for (int i = 0; i < n; i++)
     {
         Vector3 spawnPosition = SpawnPosition[Random.Range (0,SpawnPosition.Count)];
         Instantiate (Sprites[Random.Range (0,Sprites.Count)] , spawnPosition, transform.rotation);
     }

 }

How can I do to eliminate positions that have already been chosen randomly?

I'm totally wrong and I have to do it another way?

Comment
Add comment · Show 1
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 nu-assets · Jan 31, 2015 at 12:25 PM 0
Share

Check this solution: Spawn Position Check

3 Replies

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

Answer by jenci1990 · Jan 31, 2015 at 04:45 PM

 void SpawnEnemy() {
         List<Vector3> positionList = SpawnPosition;
         for (int i = 0; i < n; i++) {
             if (positionList.Count == 0) return;
             int index = Random.Range(0, positionList.Count);
             Vector3 spawnPosition = positionList[index];
             Instantiate(Sprites[Random.Range(0, Sprites.Count)], spawnPosition, transform.rotation);
 
             //Remove the position from list what was used
             positionList.RemoveAt(index);
         }
     }
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 talyon · Feb 01, 2015 at 11:30 AM 0
Share

Thanks a lot it works perfectly

avatar image
0

Answer by Andreapigna · Jan 31, 2015 at 02:16 PM

You could create a array where you put the various Vector3 you randomly create. Then, when another spawnposition is selected, you compare it with all the elements in the array. if it already exists, it'll create another one randomly (try using a for cycle to compare all the vectors)

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 talyon · Jan 31, 2015 at 02:26 PM 0
Share

Are you italian?

avatar image Andreapigna · Jan 31, 2015 at 02:46 PM 0
Share

Yeah, is it so obvious? :)

avatar image talyon · Jan 31, 2015 at 02:48 PM 0
Share

i'm italian too can you give me some way to chat? I need a little help if you want to

avatar image Andreapigna · Jan 31, 2015 at 03:33 PM 0
Share

yeah, you could contact me on skype at andrea.signorelli13 (i'm not on very much though)

avatar image
0

Answer by DESIMA · Jan 31, 2015 at 07:27 PM

yeah so it get positioned on top of each other because you are getting random numbers that could be the same ones allready taken.

to get unique numbers one way could be to create a list of random numbers where all are unique and then use that list.

you can use a set whish is called HashSet in C#

http://stackoverflow.com/questions/5157787/collection-that-allows-only-unique-items-in-net

so make your random numbers and input the output into the hashset. Then iterate through that set.

you can also just put your random numbers in a list and then for every new number check your list to see if the number allready is there.

However if you are dealing with many numbers, over 1000 of length then it would take more time using that simple approach.

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

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

23 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

Related Questions

Problem with ScreenToWorldPoint and touches (C#) 2 Answers

Object Spawner 1 Answer

Unity 2D Rouge-Like game tutorial help 0 Answers

How can I use foreach and a list of vector3s to reset the positions of all child objects? 1 Answer

How to Spawn after checking if the clones are destroyed. 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