- Home /
Passing parameters from an animation event
All my questions lately are in a similar theme, sorry.
I've been successfully using animation events to call a function in a script that: 1) plays a sound, and 2) (to keep things as isolated as possible) calls a function in another script to display an animated GUITexture. Works perfectly.
I've now attempted to use the same system for my NPCs footsteps. I've duplicated the "walk" animation using Rune's script from this post and I amended my NPC's animation script to make sure it is "walk_copy" that is now being played.
I realised that my existing soundFXControl
script would always point to the same GUITexture object assigned in the editor, but I have one for each footstep so I have amended my code to receive a parameter from the animation event. This parameter is a string, either "footleftGUITex" or "footrightGUITex".
Since I still need to use the same script for other animation events which won't have these parameters, I added an if
statement to check if the parameter (called whichFoot) is not null, and if it isn't then set the value of guiObj (the variable I am using to refer to which guiTexture I am addressing) to the value of whichFoot. The modified code is below.
var audioVolume = 1.0; var collisionSoundEffect : AudioClip;
var guiObj : GameObject;
private var inRange : boolean = false;
function playSound(whichFoot : String){ audio.volume = audioVolume; audio.clip = collisionSoundEffect; audio.Play();
if(!whichFoot){
guiObj = GameObject.Find(whichFoot);
}
print(whichFoot);
otherscript = guiObj.GetComponent("guiAnim2");
otherscript.PlayAnim();
}
@script RequireComponent(AudioSource)
However the value of guiObj
never seems to change so I am seeing the same footstep graphic for both footsteps.
I've confirmed that it doesn't change using the Debug.Log()
command, and observing it in the Editor, I can see that it remains at whichever object I set initially (Unity complains if I don't set it). It doesn't seem to be affected by the parameters being sent at all.
Removing the if
statement actually allows it to work, so the parameters are being correctly passed, but this throws a NulLReferenceException error for other objects which don't have a value for whichFoot
. I know that my existing code works so the problem lies with my line if(!whichFoot)
but I cannot see what it is. My intention is that if whichFoot
is null the line that assigns it to guiObj
will be bypassed completely.
I'd appreciate any help someone could give with this. Thank you.
Answer by straydogstrut · Apr 05, 2010 at 11:03 AM
Okay I got it working. I had to change the if(!whichfoot)
line to expressly compare itself with "footleftGuiTex" and "footrightGuiTex". Before that I tried if(!whichFoot == "")
but that didn't work either. The method i'm using now works fine, and isn't a problem in my case, but it would be a bit clunky if I was looking out for more than two parameters. Amended code is:
var audioVolume = 1.0; var collisionSoundEffect : AudioClip;
var guiObj : GameObject;
private var inRange : boolean = false;
function playSound(whichFoot : String){ audio.volume = audioVolume; audio.clip = collisionSoundEffect; audio.Play();
if(whichFoot == "footleftGUITex" || whichFoot == "footrightGUITex"){
guiObj = GameObject.Find(whichFoot);
}
Debug.Log(guiObj.name);
//Debug.Log(whichFoot.GetType());
otherscript = guiObj.GetComponent("guiAnim2");
otherscript.PlayAnim();
}
@script RequireComponent(AudioSource)
Answer by Peter G · Apr 04, 2010 at 08:26 PM
GameObject.Find takes a string.
Hi Peter, yes I know it does and as far as I am aware I am declaring the inco$$anonymous$$g parameter as a String in the code above. I tried adding quotation marks to the parameter declared in the animation event but that makes no difference. If you can clarify what you meant i'd be grateful. Thanks.
I've used Debug.Log(whichFoot.GetType()); to confirm that whichFoot, while only existing within the playSound() function, is really a string, so i'm not sure what i'm missing.
Your answer
![](https://koobas.hobune.stream/wayback/20220612144526im_/https://answers.unity.com/themes/thub/images/avi.jpg)