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 lukas77i · Oct 01, 2011 at 09:28 AM · inspectorstringcomponentassign

Accessing a function within a script assinged in inspector.

Hi,

how can I do something like the following?

 var fnToRun: String;
 var script: String;
 
 function Start()
 {
     GetComponent(script).StartCoroutine(fnToRun);    
 }
 

Basically, I need to be able to specify the script name and the function in the inspector. However, the snippet above doesn't work and gives me an error saying: 'StartCoroutine' is not a member of 'UnityEngine.Component'.

How do I achieve this? Any help is much appreciated.

Edit:

I need to be able to call non-static functions.

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 Martijn Hendriks · Oct 01, 2011 at 02:03 PM

You need to typecast the returned component to a MonoBehaviour. I'm no javascript coder, but in C# it looks like

 ((MonoBehaviour)this.GetComponent(script)).StartCoroutine(fnToRun);

( code not tested )

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 Bunny83 · Oct 01, 2011 at 02:15 PM 0
Share

Jep that's how it looks like in a single line :D but in UnityScript (JS) you can't use the c-style cast. You have to use the as-cast:

 (GetComponent(script) as $$anonymous$$onoBehaviour).StartCoroutine(fnToRun);

But complex-single-line commands doesn't help to read the code ;)

avatar image
0

Answer by nasapc123 · Oct 01, 2011 at 01:25 PM

i would use send message myself:

 var fnToRun: String;
 var script: String;
 
 function Start()
 {
 var scriptObject =  GetComponent(script);
 scriptObject.SendMessage(fnToRun);   
 }
Comment
Add comment · Show 4 · 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 Bunny83 · Oct 01, 2011 at 02:21 PM 0
Share

Getting the script component in quite pointless in the case of Send$$anonymous$$essage since Send$$anonymous$$essage will be sent to all scripts on this object.

avatar image lukas77i · Oct 01, 2011 at 06:10 PM 0
Share

ok I'll try that thanks. But isn't send message very expensive on iPhone? I'm planning on calling functions through this method a lot.

avatar image nasapc123 · Oct 01, 2011 at 11:22 PM 0
Share

haha didnt realise it was the same object :D all good,just use Send$$anonymous$$essage(fnToRun);

avatar image sotirosn · Jun 13, 2013 at 10:40 PM 0
Share

Send$$anonymous$$essage does not yield

avatar image
0

Answer by Bunny83 · Oct 01, 2011 at 02:12 PM

The problem is that GetComponent returns a Component-reference but only MonoBehaviour components have the StartCoroutine function. You need to cast it to MonoBehaviour to access the function.

var fnToRun: String; var script: String;

function Start() { var scriptObject : MonoBehaviour = GetComponent(script); scriptObject.StartCoroutine(fnToRun); }

However that's in general not a good approach. You should work with direct references and not with string-based reflection-like calls.

If you really need the function to be dynamically called, at least use a reference to the script:

var fnToRun: String; var script: MonoBehaviour; // Drag the script-component onto the variable.

function Start() { script.StartCoroutine(fnToRun); }

Or when the script is located on the same object it's probably easier to use SendMessage:

var fnToRun: String;

function Start() { SendMessage(fnToRun); }

edit
Since it seems a lot of people are not familiar with the Unity editor here is a tutorial how to add another inspector and how to assign a component to a variable:

Add another Inspector

Assign a script to a variable

You can also drag the GameObject, but Unity will take the first Component that matches the required type. If you have only one script attached you can just drag the GameObject (Same happens with a transform component when dragging an GameObject onto a variable)

Comment
Add comment · Show 11 · 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 Martijn Hendriks · Oct 01, 2011 at 03:50 PM 0
Share

This is actually also the case with the method. In C# you can use delegates, but I have no idea how this is done in javascript.

avatar image lukas77i · Oct 01, 2011 at 06:19 PM 0
Share

I tried this approach but I can't drag the script onto the script variable in inspector. I can only drag a gameobject. What if my gameobject has more scripts assigned. Thanks.

avatar image Martijn Hendriks · Oct 01, 2011 at 07:54 PM 0
Share

Just for basic understanding! Why do you want to have the inspector have this information about the script and its function? Because you already know which method to call from which script in code, right? If you want to dynamically choose which method to call from a script, maybe a simpler solution would be to make a switch.

avatar image lukas77i · Oct 01, 2011 at 08:05 PM 0
Share

The purpose of this is a multi-use touch button script. Let's say I have a guitexture and I assing this button script. This script lets me enter the script name and function name to execute. Therefore I can use the same button sctipt to run any function in any script. I have tried switch but it's unclear and always requires updating as I add more functions and scripts.

I hope this helps. Oh, and thanks for helping.

avatar image Martijn Hendriks · Oct 01, 2011 at 10:07 PM 0
Share

Did you try the solution of $$anonymous$$e, or was this also malfunctioning? Since it is impossible to link a $$anonymous$$onoBehaviour to an inspector variable, I think the best way to go is the string based search.

Show more comments

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

7 People are following this question.

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

Related Questions

Inspector vs Script: Component best practice? 1 Answer

Accessing a function through its name stored in a string JS 2 Answers

"transform" in a static function 1 Answer

Add material component to object script-wise 4 Answers

Inspector Definitions for New Script Components Problem 0 Answers


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