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
2
Question by FTheCloud · Aug 19, 2011 at 09:05 AM · randomspawnfunction

Choosing random function

Say you have:

 function Start() {
 SlowSpawn();
 MediumSpawn();
 FastSpawn();
 }

How would you randomly choose one of those functions to run?

Take it one step further and you could have:

 function Start() {
 for (var a : int = 0; a<AmountOfSlow; a++) {
 yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax)); 
 SlowSpawn();
    }
 for (var b : int = 0; b<AmountOfMedium; b++) {
 yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax)); 
 MediumSpawn();
    }
 for (var c : int = 0; c<AmountOfFast; c++) {
 yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax)); 
 FastSpawn();
    }
 }

and each one spawns a certain number of enemies.

Now how would you randomly choose a function to run, run that function, and then randomly choose another one of the functions until all the enemies have been spawned.

The problem with this is that if you write list of functions, of course it runs them all at once. But I want them to be running individually and one after the other.

Thanks for any help

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
11
Best Answer

Answer by Eric5h5 · Aug 19, 2011 at 06:31 PM

Use an array of functions, and choose randomly from the array like you would with any other array:

 var functions = [SlowSpawn, MediumSpawn, FastSpawn];
 
 function Start () {
     functions[Random.Range(0, functions.Length)]();
 }
Comment
Add comment · Show 15 · 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 FTheCloud · Aug 19, 2011 at 08:00 PM 0
Share

Works great with the first part of my question but doesn't work with, answer or address the second and most important part of my question.

avatar image Techsuport · Aug 19, 2011 at 08:10 PM 0
Share

But how would you type cast a function? its not a string, and all the examples I've seen are dynamically typed. How would you script something like delegates, and having to typcast a function, that is statically typed, and will work with #pragma strict?

avatar image FTheCloud · Aug 19, 2011 at 08:14 PM 0
Share

if else statements, Derp

avatar image Eric5h5 · Aug 19, 2011 at 10:00 PM 1
Share

The code I wrote is statically typed and works fine with #pragma strict. If you need to specify a public variable with no default value, the type is "function()", including whatever argument types it has. e.g.,

 var myFunction : function():void;

So a function that doesn't have arguments and returns void is "function():void" and a function that takes two ints and returns a string is "function(int, int):String". Which means you can't mix functions that take different arguments in the same array (unless you use an untyped array such as Array or ArrayList).

As for the second part of your question, you should really be asking one question per question.

avatar image Techsuport · Aug 19, 2011 at 10:29 PM 0
Share

thanks Eric that was really helpful, but how would i alter what functions are chosen in the inspector? out of curiosity

Show more comments
avatar image
2

Answer by CHPedersen · Aug 19, 2011 at 09:13 AM

Your friend in need is the class System.Random. It's a pseudo-random generator that you can use along with a switch to randomly execute one of the functions every time it's called. Here's an example (In C# though, hope you can translate):

 System.Random randomizer = new System.Random();
 int funcToChoose = randomizer.Next(3);
 
 switch (funcToChoose)
 {
     case 0:
         // Fire the first function
         break;
     case 1:
         // Fire the second function
         break;
     case 2:
         // Fire the third function
         break;
 }

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 Eric5h5 · Aug 19, 2011 at 06:34 PM 0
Share

Unity's Random function is easier to use in most cases.

avatar image
1

Answer by FTheCloud · Aug 20, 2011 at 04:36 AM

Ok, My question was pretty tough to understand exactly what I meant without me point at my screen so I stopped being lazy and wrote it myself. Couldn't have done this without a part of Eric's Answer though. Thank You Eric.

With this your able to have, say, three different types of enemies and then define how much you want each one to spawn. Then the script spawns each one, individually, and with a certain time between each spawn until there's no more left to spawn.

That's what my question was really trying to get at.

For anyone who runs into the same problem Use this as reference.

 var SpawnPoints : Transform[]; 
 var SlowEnemy : GameObject; 
 var MediumEnemy : GameObject; 
 var FastEnemy : GameObject; 
 var AmountOfEnemiesTotal : int;
 var WaitTime : int = 1;  
 var NumberOfSlowEnemies : int;
 var NumberOfMediumEnemies : int;
 var NumberOfFastEnemies : int;
 var SlowTrue : boolean = true;
 var MediumTrue : boolean = true;
 var FastTrue : boolean = true;
 private var functions = [SlowSpawn, MediumSpawn, FastSpawn];
 
 
 function FixedUpdate(){
 if (NumberOfSlowEnemies == 0){
 SlowTrue = false;
 }
 if (NumberOfMediumEnemies == 0){
 MediumTrue = false;
 }
 if (NumberOfFastEnemies == 0){
 FastTrue = false;
 }
 if (SlowTrue == false){
 functions = [MediumSpawn, FastSpawn];
 }
 if (MediumTrue == false){
 functions = [FastSpawn, FastSpawn];
 }
 if (FastTrue == false){
 functions = [MediumSpawn, SlowSpawn];
 }
 if (SlowTrue == false && MediumTrue == false){
 functions = [FastSpawn];
 }
 if (SlowTrue == false && FastTrue == false){
 functions = [MediumSpawn];
 }
 if (MediumTrue == false && FastTrue == false){
 functions = [SlowSpawn];
 }
 if (MediumTrue == false && FastTrue == false && SlowTrue == false){
 StopAllCoroutines();
 }
 }
 
 function Start() {
 AmountOfEnemiesTotal = NumberOfSlowEnemies+NumberOfMediumEnemies+NumberOfFastEnemies;
 StartCoroutine("FunctionArc");
 }
 
 function FunctionArc(){
 yield WaitForSeconds(WaitTime);
 if (AmountOfEnemiesTotal>0){
 StartCoroutine("ChooseFunction");
 }
 }
 
 function ChooseFunction(){
 functions[Random.Range(0, functions.Length)]();
 StopCoroutine("ChooseFunction");
 }
 
 
 function SlowPass(){
 if (SlowTrue && NumberOfSlowEnemies>0){
 StartCoroutine("SlowSpawn");
    }
 }
 
 function MediumPass(){
 if (MediumTrue && NumberOfMediumEnemies>0){
 StartCoroutine("MediumSpawn");
    }
 }
 
 
 function FastPass(){
 if (FastTrue && NumberOfFastEnemies>0){
 StartCoroutine("FastSpawn");
    }
 }
 
 
 function SlowSpawn() { 
       NumberOfSlowEnemies -= 1;
       var pos: Transform = SpawnPoints[Random.Range(0, SpawnPoints.length)]; 
       Instantiate(SlowEnemy, pos.position, pos.rotation); 
       StartCoroutine("FunctionArc"); 
       StopCoroutine("SlowSpawn");
    }
 
 function MediumSpawn() {
       NumberOfMediumEnemies -= 1;
       var pos2: Transform = SpawnPoints[Random.Range(0, SpawnPoints.length)]; 
       Instantiate(MediumEnemy, pos2.position, pos2.rotation); 
       StartCoroutine("FunctionArc"); 
       StopCoroutine("MediumSpawn");
    }  
 
 function FastSpawn() { 
       NumberOfFastEnemies -= 1;
       var pos3: Transform = SpawnPoints[Random.Range(0, SpawnPoints.length)]; 
       Instantiate(FastEnemy, pos3.position, pos3.rotation); 
       StartCoroutine("FunctionArc"); 
       StopCoroutine("FastSpawn");
    } 
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 testure · Aug 20, 2011 at 05:00 AM 1
Share

If you couldn't have done it without eric's answer, you should've posted this as a comment and accepted his answer so he gets credit for it...

just sayin.

avatar image FTheCloud · Aug 20, 2011 at 05:36 AM 0
Share

Gave him a thumbs up, just marked this one right because its the exact answer to the question.

This is a database right?

avatar image meat5000 ♦ · Jul 19, 2015 at 10:54 AM 0
Share

Your full solution is a direct extension of Eric's answer.

You can have a +1 on the Question for it being a good one.

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

8 People are following this question.

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

Related Questions

Call a function multiple times 2 Answers

Spawn at random? 1 Answer

Why does inside unit circle use the world point? 2 Answers

How do I make a game object spawn several times in semi-random locations? 1 Answer

Quaternion.identity problem at random spawn 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