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 Taelyth · May 17, 2011 at 10:37 PM · gameobjectrandomspawnpointsdifferentplace

Multiple spawn points without spawning in the same place.

I'm trying to spawn some GameObjects to random spawn points, but they are spawning in the same spawnpoint, how can I make then spawn in different spawnpoints?

I'm using C#. Here is the code:

public Transform[] spawns; public GameObject Bone; public int amountThings = 2; public int i = 0;

public void Start() { Spawn(); }

public void Spawn() { for (i = 0; i < amountThings; i++) {

     Transform pos = spawns[Random.Range(0, spawns.Length)];


     Instantiate(Bone, pos.position, pos.rotation);


 }


}

Sorry about my English.

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

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

Answer by Bunny83 · May 17, 2011 at 11:28 PM

Well, if you just want to make sure during a spawn event (where you spawn multiple objects at the same time) that every object gets a unique spawn point you can use a copy of your list and remove the used spawn points. That would be the easiest way. If you want to spawn continuously new objects and some of the used spawn points are free again, you have to use either some sort of trigger around each spawnpoint to tell if it's "free", or if you're sure that the point is free after e.g. 5 seconds just give them a timeout.

note: no syntax check performed ;) You will also need to add the System.Collections.Generic; namespace.

public Transform[] spawns; public GameObject Bone; public int amountThings = 2; public int i = 0;

public void Start() { Spawn(); }

public void Spawn() { List<Transform> freeSpawnPoints = new List<Transform>(spawns); for (i = 0; i < amountThings; i++) { if (freeSpawnPoints.Count <=0) return; // Not enough spawn points int index = Random.Range(0, freeSpawnPoints.Count); Transform pos = freeSpawnPoints[index]; freeSpawnPoints.RemoveAt(index); // remove the spawnpoint from our temporary list Instantiate(Bone, pos.position, pos.rotation); } }

Comment
Add comment · Show 3 · 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 Taelyth · May 18, 2011 at 12:20 AM 0
Share

I'm trying to use this code but have an error here:

System.Collections.Generic.List ' does not contain a definition for Length' and no extension method Length' of type `System.Collections.Generic.List ' could be found.

I'm using System.Collections.Generic

avatar image Taelyth · May 18, 2011 at 12:26 AM 0
Share

I changed Length for Count and now it is ok! Thank you! =D

avatar image Bunny83 · May 18, 2011 at 03:02 AM 0
Share

Sorry :D my fault, i just changed the name from your code. You're right it's Count. I will change my answer ;)

avatar image
0

Answer by Antony-Blackett · May 17, 2011 at 11:05 PM

I assume you mean they are in the same place each time you run your game?

If so the the trouble you're having is that the Random seed is the same every time you run your game. This isn't really a bad thing, it means that you can easily reproduce and debug issues that you find resulting from random numbers. But if you want your game to be random every time you play you'll have to use something like your systems clock to set the seed on the Random class.

try adding a line to your start function like this:

void Start()
{
    Random.seed = System.DateTime.Now.Second;
    Spawn();
}
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 Bunny83 · May 17, 2011 at 11:14 PM 0
Share

I don't think he had this in $$anonymous$$d ;). I guess that his issue is that the script spawns sometimes two or more objects at the same spawnpoint because Random.Range returned the same number again.

avatar image Taelyth · May 18, 2011 at 12:14 AM 0
Share

Bunny83 answered correctly. The objects in the same spawnpoint is because they returned the same number.

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

No one has followed this question yet.

Related Questions

how to freeze a random position? 1 Answer

Cards To Hand 0 Answers

Two different random numbers 2 Answers

How to join a specific image to random button...?,Hello everybody, 0 Answers

Randomly instantiating a list of objects at a random position. 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