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 vittu1994 · Mar 09, 2015 at 11:36 AM · c#arrayrandom.rangefunctions

randomize a function from a array (C#)

i have a A.I script with different functions for the conditions on which the enemy shall walk. I want to make a Invoke Repeating() on a function called RandomFunction(). This function shall do a random.range and select randomly from the array of functions, which are [5].

with invokerepeating repeating every 1 second a new function will be called and it should make the player walk randomly around every 1 second.

How would i go about making an array of functions in c#, and how can i use random.range in this array.

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
1

Answer by RabidCabbage · Mar 09, 2015 at 01:18 PM

You can store functions in an array using the Func<> type, eg:

Define an array of 5 functions (You can't use void as the return type afaik, so bool will have to do)

 Func<bool>[] AnimList = new Func<bool> [5];

Create multiple functions that return the desired type (in this case bool)

 bool Idle() { Debug.Log("Idle"); return true; }
 bool Yawn() { Debug.Log("Yawn"); return true; }

Assign the function's to array indexes

 AnimList[0] = Idle;
 AnimList[1] = Yawn;

Then you can send the Random.Range int's directly into the array index

 AnimList[UnityEngine.Random.Range(0, AnimList.Length)]();
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
1

Answer by fafase · Mar 09, 2015 at 01:18 PM

 private System.Action myUpdate = ()=>{}; 
 private System.Action [] actions = null;
 void Start(){
     actions = new System.Action []{MethodA,MethodB, MethodC, MethodD, MethodE };
     InvokeRepeating("Randomizer", 1.0f);
 }
 
 void Udpate()
 {
    myUpdate();
 }
 
 void Randomizer(){
     int rand = Random.Range(0, actions.Length);
     myUpdate = actions[rand];
 }
 
 void MethodA(){ Debug.Log("A");}
 void MethodB(){ Debug.Log("B");}
 void MethodC(){ Debug.Log("C");}
 void MethodD(){ Debug.Log("D");}
 void MethodC(){ Debug.Log("E");}

myUpdate is a delegate, so pretty much a reference to a method. What is done in the declaration is just a fancy way to prevent null reference so a default method is added (you may print something to actually see that something is wrong).

The Update just calls that delegate so indirectly it is calling the method it is referring to. The Randomizer method just gets a random method out of an array of methods.

In case of, Action is just a short way for generic delegate.

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 vittu1994 · Mar 09, 2015 at 01:29 PM 0
Share

i got the same error in this example as when i tried myself to do a array with a certain namespace/type:

Assets/Scripts/Enemy$$anonymous$$ovement1.cs(15,17): error CS0246: The type or namespace name `Action' could not be found. Are you missing a using directive or an assembly reference?

its refering to the line: actions = new Action []{$$anonymous$$ethodA,$$anonymous$$ethodB, $$anonymous$$ethodC, $$anonymous$$ethodD, $$anonymous$$ethodE }; and saying that "Action[]" is invalid.

avatar image fafase · Mar 09, 2015 at 01:48 PM 0
Share

Action is part of the System namespace, I added the explicit reference in my code or you can add a :

   using System;

at the top.

avatar image vittu1994 · Mar 09, 2015 at 02:00 PM 0
Share

Thank you!

avatar image
0

Answer by siaran · Mar 09, 2015 at 01:32 PM

I don't see why you want to put your functions in an array here - it's probably possible to hack something together using delegate variables (not sure, never tried), but I think that's overcomplicating things - instead, why not simply write a switch, something like

 void ExecuteFuntionByIndex(int index){
  switch(index){
   case 0:
    FunctionZero();
    break;
  case 1: 
   FunctionOne();
   break;
  case 2:
   FunctionTwo();
   break;
  //..etc. for how many functins you have
  default:
   //Default to function 0 when you get an invalid index
   FunctionZero();
   break;
  }
 }




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 vittu1994 · Mar 09, 2015 at 01:36 PM 0
Share

This works aswell, but then the actions will then follow a specific order. Now im calling all my functions thats specified in the switch statement ins$$anonymous$$d of generating a random function after another.

but again this will probably be my plan B, thanks.

avatar image Dreamblur · Mar 09, 2015 at 02:18 PM 0
Share

It's implied that you would be randomizing the value of the variable named index, so it works as you deem necessary.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Why is Random.Range not working in array 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Create an array that chooses between four random numbers (C#) 3 Answers

(C#) Help with spawning a random GameObject 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