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 jabaruoky · Feb 06, 2015 at 01:23 PM · c#spawnrandom.range

Not repeating random

I have a code that randomly spawn enemies. How can i make so that the enemies spawed dont repeat?

The code goes like this

 IEnumerator SpawnWaves ()
         {
                 yield return new WaitForSeconds (startWait);
                 while (true) {
                         for (int i = 0; i < hazardCount; i++) {
                                 platform = Random.Range (1, 4);
                                 
                                 switch (platform) {
                                 case 1: 
                                         Instantiate (prefab1, new Vector3 (0, 6, 0), Quaternion.identity); 
                                         break;
                                 case 2:
                                         Instantiate (prefab2, new Vector3 (0, 6, 0), Quaternion.identity);
                                         break;
                                 case 3: 
                                         Instantiate (prefab3, new Vector3 (0, 6, 0), Quaternion.identity);
                                         break;
                                 }
 
                                 if (!dead) {
                                         yield return new WaitForSeconds (Random.Range (spawnWaitStart, spawnWaitEnd));
                                 }
                                 if (dead) {
                                         yield return new WaitForSeconds (100);
                                 }
                         }
                 }
         }


Thank you!

Comment
Add comment · Show 4
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 AngryBurritoCoder · Feb 06, 2015 at 01:26 PM 0
Share

what do you mean not repeat ?

avatar image fddefdef · Feb 06, 2015 at 01:30 PM 0
Share

your answer doesn't make sense like this. You mean that you want to spawn randomly only the three different prefabs? Cause it's the only way for not repeating them.

avatar image jabaruoky · Feb 06, 2015 at 03:10 PM 0
Share

I mean: If i have objects 1 2 3 i don't want them to apear 1 1 1 2 3 3 3 3 but ins$$anonymous$$d 1 3 2 1 3 1 2 3. Not the same one after the other

tks

avatar image jabaruoky · Feb 06, 2015 at 05:57 PM 0
Share

Worked like a charm @Bonfire Boy.

Thank you all

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Mmmpies · Feb 06, 2015 at 02:42 PM

Well if Answers can stay up long enough I'll post this (4th attempt!).

This is just example code but it stores a List of three numbers and removes the one picked. When it gets to 0 it rebuilds the list. Only an example so if you can't work out what to do with the list let me know.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;        // IMPORTANT ADD THIS TO USE LISTS
 
 public class ListTest : MonoBehaviour {
 
     private List<int> EnemyCountList = new List<int>();
     private int myInt;
 
     // Use this for initialization
     void Start () {
         LoadEnemyList();
     }
     
     // Update is called once per frame
     void Update () {
     
         myInt = Random.Range (0,EnemyCountList.Count);
         Debug.Log ("How many in the list = " + EnemyCountList.Count + "  Selected Int = " + EnemyCountList[myInt]);
 
         // instantiate your enemy your enemy number is EnemyCountList[myInt];
 
         EnemyCountList.Remove(EnemyCountList[myInt]);
         if(EnemyCountList.Count == 0)
             LoadEnemyList();
 
     }
 
     void LoadEnemyList()
     {
         EnemyCountList.Add(1);
         EnemyCountList.Add(2);
         EnemyCountList.Add(3);
     }
 }


EDIT

OK so you just don't want the same one as last time but any other order is OK?

 private int lastLoaded = 0;
 
 
 // after platform = Random.Range(1,4)'
   if(platform == lastLoaded)
      int tempRand = Random.Range(0, 2);
      
      switch (platform) {
      case 1:
              if(tempRand = 0)
                  platform = 2;
                  else
                  platform = 3;
                  break;
      case 2:
              if(tempRand = 0)
                  platform = 1;
                  else
                  platform = 3;
                  break;
      case 3:
              if(tempRand = 0)
                  platform = 1;
                  else
                  platform = 2;
                  break;
      }
 
  // after you instantiate the object
  lastLoaded = platform;


add that and it should stop the previous enemy from instantiating, not tested that code though so watch for errors.

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 Bonfire-Boy · Feb 06, 2015 at 03:58 PM 0
Share

That's a bit more of a constraint than asked for (it won't allow the example acceptable sequence given in the comment - although to be fair I just noticed that the comment came after your answer!).

All he needs to do is keep track of which one was generated last time, and either flip a coin to choose between the other two or (more easily extended to larger sets) throw away and randomise again if the same one is picked twice in a row.

avatar image Mmmpies · Feb 06, 2015 at 04:03 PM 0
Share

Is anyone else having issues with Answers, I posted my flip a coin method plenty before your Comment on flipping coins @Bonfire Boy, keeps kicking me out as well.

avatar image
0

Answer by Bonfire-Boy · Feb 06, 2015 at 04:04 PM

Just keep track of what the last one was, and re-randomise if you pick it again next time.

 int lastPlatform=-1;
 while (true) 
 {
     for (int i = 0; i < hazardCount; i++) {
     platform = Random.Range (1, 4);
     while (platform == lastPlatform)
     {
         platform = Random.Range(1,4);
     }
     lastPlatform = platform;
     switch (platform) {
 
 // rest of it as in the question.
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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

(C#) Help with spawning a random GameObject 1 Answer

How to make enemy prefab spawn at random times between 1.0f to 10.0f. 1 Answer

move gameobject into a random position and the spawn an enemy 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