Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by jahroy · Apr 13, 2011 at 09:22 PM · variablesfunctions

Referencing functions from other scripts as variables

I would like to use an editor script to initialize a list of buttons that will perform various actions on objects in my scene. I am wondering how to assign functions to these buttons as varialbes if the functions are defined outside the editor script.

More specifically:

I have a GUI controller script (SomeNeatScript) that performs actions on GameObjects based on GUI events. I would like this script to contain an array of buttons that will each call a different function when clicked. I would like to assign functions to the buttons in an initialization routine that will be called from an Editor Script. I would like the assigned functions to be defined in the GUI script.

Some code:

/* from some editor script */

static function initializeButtons () { var myScript = Selection.activeTransform.GetComponent(SomeNeatScript);

 var buttonNames = new Array(
     "buttonA",
     "buttonB", 
     "buttonC",
     "buttonD"
 );

 var buttonFunctions = new Array(
     myScript.functionA,
     myScript.functionB,
     myScript.functionC,
     myScript.functionD
 );

 var someButtons = new Array();

 for ( var i = 0; i < buttonNames.length; i ++ ) {
     var freshButton = new ActionButton(buttonNames[i], buttonFunctions[i]);
     someButtons.Push(freshButton);
 }

 myScript.buttonList = someButtons.ToBuiltin(ActionButton);

}

/ ActionButton definition /

class ActionButton { var name : String; var actionFunction : Function;

 function ActionButton ( myName : String, myFunc : Function )
 {
     name            =  myName;
     actionFunction  =  myFunc;
 }

}

/ from SomeNeatScript /

var buttonList : ActionButton [];

function OnGUI () { for ( var currentButton : ActionButton in buttonList ) { if ( GUI.Button(somePosition, currentButton.name) ) { currentButton.actionFunction(); } } }

function functionA () { Debug.Log("I am function a"); }

function functionB () { Debug.Log("I am function b"); }

. . .

Ideally I would like to reference the names of the assigned functions dynamically (by building Strings based on the button's name) but would settle for the approach outlined above.

Any help would be greatly appreciated...

Edit

As a workaround I can assign the functions in the Start function of my GUI script, but I'd still like to know if it can be done from an editor script.

And I'd REALLY like to know how to dynamically reference functions by name so I could skip this whole process...

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

4 Replies

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

Answer by marceloroca · Apr 13, 2011 at 09:57 PM

Try to use Component.SendMessage. Or, you can use reflection, but is more complicated.

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
2

Answer by Statement · Apr 13, 2011 at 09:58 PM

  • You can make use of SendMessage.
  • You could make use of a table of names and look methods up by name in a prebuilt dictionary.
  • You could make use of reflection to look up methods by name of an object, to later invoke.
  • You could create your own editor/inspector to ease this process.

A very basic example of reflection to call a method on an unknown object by name:

function Call(var obj : System.Object , var message : String) {
    obj.GetType().GetMethod(message).Invoke(obj, null);
}

This search for a public method named message on the obj, and calls it without parameters. Any failure will throw an exception.

Example usage:

var obj = Selection.activeTransform.GetComponent(SomeNeatScript);
Call(obj, "buttonA"); // Attempts to call 'function buttonA()'
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 Statement · Apr 13, 2011 at 10:01 PM 1
Share

Reflection is the most powerful way of getting the methods you want to use. Reflection means for example that you discover members of types, like a method. Then you can invoke that method, given an object and optional parameters.

avatar image Statement · Apr 13, 2011 at 10:10 PM 0
Share

Reflection can also be used to present the user with a set of functions that could be called, yielding a richer editor.

avatar image
0

Answer by jahroy · Apr 13, 2011 at 10:06 PM

Awesome. Looks like SendMessage will work quite well. Thanks very much!

Unity rules!

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 Statement · Apr 13, 2011 at 10:09 PM 0
Share

Just know that all scripts on the same object will get the call (if you have same function names on several scripts), and you can only use that on game objects and components. As long this is no problem I'd also choose this solution.

avatar image
0

Answer by yester30 · Mar 28 at 01:38 PM

I'm here long after the war, but what about

 public MonoBehaviour MonoBehaviourToCall; // the script component you want to call
 public string functionName = ""; //name of the desired function to call there

fill those 2 refs and then later call it with

 MonoBehaviourToCall.Invoke(functionName, 0f);

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

1 Person is following this question.

avatar image

Related Questions

how to access variables in a function script attached to a clone 1 Answer

problems accessing variables and functions in another script 1 Answer

Can I pass a variable to a function to be assigned in c# 2 Answers

Referncing variables such as OnTriggerEnter(Collider other) thumb rule? 1 Answer

Change a variable via functions 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