- Home /
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();
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.
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;
}
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;
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;
}
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'.
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.
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);
Your answer
Follow this Question
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