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
1
Question by straydogstrut · Apr 04, 2010 at 07:31 PM · if-statementsparametersanimationevent

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.

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 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)

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 Peter G · Apr 04, 2010 at 08:26 PM

GameObject.Find takes a string.

Comment
Add comment · Show 3 · 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 straydogstrut · Apr 04, 2010 at 09:54 PM 0
Share

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.

avatar image straydogstrut · Apr 04, 2010 at 09:59 PM 0
Share

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.

avatar image Peter G · Apr 06, 2010 at 01:28 AM 0
Share

Oops, missed that.

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

No one has followed this question yet.

Related Questions

Using bool function with parameters 1 Answer

No colliders available for animation event parameter? Why? 0 Answers

Enumerate AnimationEvents 3 Answers

Animation events won't trigger if animation time set manually 1 Answer

Perform an action on animator state transition? 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