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 Griffo · Jun 06, 2012 at 12:34 PM · functionvar

set a var to a function in another script

How can I set a var to a function in another script ?

The function is called "function SwingSound()" and is in a script called MouseSoundController on a object called Camera. The below works fine.

 #var mouseSound = GameObject; // Set link to the Mouse Sound Controller, I've put it on the camera
 
 mouseSound.GetComponent.< MouseSoundController >().SwingSound();


I've tried the below with no luck, I get the message (Cannot convert 'void' to 'UnityEngine.GameObject'.)

 #var mouseSound = GameObject; // Set link to the Mouse Sound Controller, I've put it on the camera
 private var myVar : GameObject;
 
 myVar = mouseSound.GetComponent.< MouseSoundController >().SwingSound();
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

2 Replies

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

Answer by aldonaletto · Jun 06, 2012 at 02:00 PM

You can declare myVar: Function, like this (other script):

var myVar: Function; // myVar is a function reference

function Start(){ // get the function reference (no parenthesis!) myVar = mouseSound.GetComponent(MouseSoundController).SwingSound; }

// and use it like this:

myVar();

You can even pass parameters and receive the function result, if you want.
NOTE: This version of GetComponent is better than the generic and string ones.

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
0

Answer by bompi88 · Jun 06, 2012 at 01:39 PM

I really don't know what you are trying to do here. But if you want to call a function in another script, you could do like this:

MouseSoundController.js:

 // this returns the AudiSource from the gameObject this script is attached
 function SwingSound () : AudioSource {
     return audio;
 }

OtherScript.js

 var mouseSound : GameObject; // The object that has the MouseSoundController attached
 private var myAudio : AudioSource;
 function Start() {
     myAudio = mouseSound.GetComponent("MouseSoundController").SwingSound();
     myAudio.Play();
 }

EDIT: The solution of the problem:

If the function SwingSound in MouseSoundController is a void and don't return a value, and it don't take any parameters, then use this:

 var mouseSound : GameObject;
 function Start () {
     var myVar: System.Action;
     var swooshSoundEvent = new AnimationEvent();
     myVar = mouseSound.GetComponent(MouseSoundController).SwingSound;
     swooshSoundEvent.functionName = myVar.Method.Name;
     Debug.Log(swooshSoundEvent.functionName);
 }

and: this is an example if the function SwingSound in MouseSoundController takes a parameter int, and return a value of float:

 var mouseSound : GameObject;
 function Start () {
     var myVar: System.Func.<int,float>; // where int is the type of variable put into the function, and float is the variable the function returns.
     var swooshSoundEvent = new AnimationEvent();
     myVar = mouseSound.GetComponent(MouseSoundController).SwingSound;
     swooshSoundEvent.functionName = myVar.Method.Name;
     Debug.Log(swooshSoundEvent.functionName);
 }

The last script is the case where SwingSound looks similar to this:

 function SwingSound (t:int) : float{
     return 1.0223; 
 }
Comment
Add comment · Show 14 · 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 Griffo · Jun 06, 2012 at 01:55 PM 0
Share

Like I said above I've got all my sound files on a script called $$anonymous$$ouseSoundController that is attached to the camera that follows the mouse, so when I need one I call it with the top code above and all works ok, but I'm trying to set up an animation event and when I put the line 3 in below I get an error.

 var swooshSoundEvent = new AnimationEvent();
     swooshSoundEvent.functionName = "mouseSound.GetComponent.< $$anonymous$$ouseSoundController >().SwingSound()";
         swooshSoundEvent.time = 0.5;
avatar image bompi88 · Jun 06, 2012 at 02:35 PM 0
Share

Then you should do something like Aldo Naletto suggested, and get the function name of that function. This because AnimationEvent.functionName is a string variable. var mouseSound : GameObject;

 function Start(){
     var myVar: Function;
     var swooshSoundEvent = new AnimationEvent();
     // get the function reference (no parenthesis!)
     myVar = mouseSound.GetComponent($$anonymous$$ouseSoundController).SwingSound;
     swooshSoundEvent.functionName = myVar.$$anonymous$$ethod.Name; //function name of function
     swooshSoundEvent.time = 0.5;

}

avatar image Griffo · Jun 06, 2012 at 06:43 PM 0
Share

Thanks for the help, If I use swooshSoundEvent.functionName = myVar(); It plays the sound clip straight away before passing it as as the animation event to the animation.

If I use swooshSoundEvent.functionName = myVar.$$anonymous$$ethod.Name; I get the error '$$anonymous$$ethod' is not a member of 'Function'.

avatar image bompi88 · Jun 06, 2012 at 07:53 PM 0
Share

Are you sure? If I use Debug.Log(swooshSoundEvent.functionName); in my script above, i get "SwingSound". And my $$anonymous$$ouseSoundController.js looks like this:


function SwingSound () {
Debug.Log("AnimationEvent SwingSound");
}

At the moment i'm using Unity3D version 3.5.0f5.

avatar image Griffo · Jun 07, 2012 at 08:42 AM 0
Share

Hi, I'm using Unity 3.5.2f2 and from the below code it gives me the error '$$anonymous$$ethod' is not a member of 'Function'.

 var myVar: Function;
     var swooshSoundEvent = new AnimationEvent();
     // get the function reference (no parenthesis!)
     myVar = mouseSound.GetComponent($$anonymous$$ouseSoundController).SwingSound;
         swooshSoundEvent.functionName = myVar.$$anonymous$$ethod.Name;
             swooshSoundEvent.time = 0.5;
 
     
     _animation[swingAnimation.name].layer = 1; // Place the Swing animation on a higher level
     _animation[swingAnimation.name].clip.AddEvent(swooshSoundEvent); // Add the event to an AnimationClip
     _animation[swingAnimation.name].blend$$anonymous$$ode = AnimationBlend$$anonymous$$ode.Additive;
         _animation[swingAnimation.name].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Once;
     _animation.CrossFade(swingAnimation.name);
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

On var enter 1 Answer

Call a Function from another Script 1 Answer

Cleaning up my item script... Passing script name to function? 0 Answers

Can't get var from other script. 1 Answer

Why can't I call function from another script? 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