Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 TheSaviour · Jul 28, 2016 at 06:20 PM · instantiatespawnspawninginvokerepeatingspawnpoints

Prevent object from spawning at a spawn point twice in a row?

I have a set of spawn points and an object that I want to spawn from a random spawn point every second. I've used the InvokeRepeating function to make this work. But now, I want to modify the code a little bit. I don't want the the object to be spawned from the same spawn point twice in a row.

Let's say I have 5 spawn points: A, B, C, D, and E. When InvokeRepeating runs, the object is spawned randomly from spawn point C. So when InvokeRepeating runs again, I don't want the object to spawn at spawn point C. It has to randomly pick another spawn point. I don't want the object to spawn at at a particular spawn point twice in a row.

Here my code:

 void Start () 
     {
         GameObject[] SP = GameObject.FindGameObjectsWithTag ("spawnPoint");
         spawnPoints = new Transform[SP.Length];
         for(int i=0; i < SP.Length; i++)
         {
             spawnPoints[i] = SP[i].transform;
         }
 
         InvokeRepeating("SpawnObject", spawnTime, spawnTime);    
     }
 
 void SpawnObject()
     {
         int spawnPointIndex = Random.Range(0,spawnPoints.Length);
         Instantiate(theObject, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
     }

Any idea what I can add to my code so solve this?

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

3 Replies

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

Answer by Jessespike · Jul 28, 2016 at 06:30 PM

If you are only concerned about the next and previous spawn points, then you can store the last spawn point index and find a new index that doesn't match the previous one.

 int prevSpawnIndex = -1;
 void SpawnObject()
 {
     int spawnPointIndex;

     do {
         spawnPointIndex = Random.Range(0,spawnPoints.Length);
     } while (prevSpawnIndex == spawnPointIndex && spawnPoints.Length > 1);
     prevSpawnIndex = spawnPointIndex;

     Instantiate(theObject, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
 }
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 TheSaviour · Mar 15, 2018 at 09:07 AM 1
Share

I totally forgot about this. Sorry for taking so long.

avatar image
1

Answer by ScaniX · Jul 28, 2016 at 06:42 PM

Something like this should work. It is a playlist approach, so each position is used exactly once, before it starts over again.

Note, that I haven't tested this as I wrote it down here, outside of Visual Studio, so there can be some typos. :)

 private List<int> pendingSpawnPoints = new List<int>();

 private void CreateSpawnList(int avoidedStartIndex) {
     List<int> tempList = new List<int>();
     for (int i=0; i<spawnPoints.Length; i++)
          tempList.Add(i);
     pendingSpawnPoints.Clear(); // it usually already is empty
     for (int i=0; i<spawnPoints.Length; i++) {
         int idx = Random.Range(0, tempList.Count);
         if (tempList[idx] == avoidedStartIndex)
             idx = (idx + 1) % tempList.Count;
         pendingSpawnPoints.Add(tempList[idx]);
         tempList.RemoveAt(idx);
     }
 }

 void Start () 
  {
      GameObject[] SP = GameObject.FindGameObjectsWithTag ("spawnPoint");
      spawnPoints = new Transform[SP.Length];
      for(int i=0; i < SP.Length; i++)
      {
          spawnPoints[i] = SP[i].transform;
      }
      CreateSpawnList(-1);
      InvokeRepeating("SpawnObject", spawnTime, spawnTime);    
  }
 
  void SpawnObject() {
      int spawnPointIndex = pendingSpawnPoints[0];
      pendingSpawnPoints.RemoveAt(0);
      if (pendingSpawnPoints.Count == 0)
          CreateSpawnList(spawnPointIndex);
      Instantiate(theObject, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
  }
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
avatar image
0

Answer by milanow · Jul 29, 2016 at 12:31 PM

I have encountered the same issue before. I solved it by searching out an algorithm that generate unrepeated number.

Supposing u have five positions Vector3[] genPoints = new Vector3[5];

You can get new positions by int index = Random.Range(0, 5);

And you create another int array

int[] intArr = new int[5]; for(int i = 0; i < 5; ++i) intArr[i] = i

Say index = 3 this time, then swap the value of intArr[3] and intArr[intArr.Count - 1] in intArr. Now you have 0, 1, 2, 4, 3 in intArray

And you got the generate point by genPoints[index]; The key point is next time you should Call index = Random.Range(0, 4) instead of Random.Range(0, 5), Now you only have "0, 1, 2, 4" these four values in intArr that can be accessed by intArr[index]

Then just reduce the random range by one for each time. Pretty simple algorithm but helpful

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

62 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 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 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

How do I spawn an object under another moving object at random in C#? 1 Answer

Can anyone help check for colliders in this spawning script so that the spawns don't overlap each other or spawn inside of walls, objetcs etc 0 Answers

Object Spawning Randomly 0 Answers

Ontriggerenter and InvokeRepeating problem 1 Answer

What's wrong with my script? 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