- Home /
Cannot convert void to String
In the below script I get the message " Cannot convert void to String " on line 5, I'm trying to link to a script called mouseSoundCntrl that holds the SwingSound I'm after, I've got lots more sounds on there and I can call them just by using something like this - mouseSoundCntrl.MouseWalkSound(); the problem is putting it in the animation event it comes back with that error, any help please would be great fully appreciated, Thanks.
else if(_characterState == CharacterState.Swing) {
// Create and set up the AnimationEvent
var swooshSoundEvent = new AnimationEvent();
swooshSoundEvent.functionName = mouseSoundCntrl.SwingSound();
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].blendMode = AnimationBlendMode.Additive; // set the blend mode to Additive so it will blend
// into whatever animation is playing when it's called
_animation[swingAnimation.name].wrapMode = WrapMode.Once;
_animation.CrossFade(swingAnimation.name); // Play the Swing animation
}
Answer by kolban · Jun 05, 2012 at 08:05 PM
You have a line which reads:
swooshSoundEvent.functionName = mouseSoundCntrl.SwingSound();
The functionName property of an AnimationEvent object is of type "String". This property is set to be the String name of a function that you want to be called. I suspect what you really want is:
swooshSoundEvent.functionName = "SwingSound";
What this will do is perform a SendMessage("SwingSound") when the animation event is fired. See:
http://unity3d.com/support/documentation/ScriptReference/AnimationEvent-functionName.html
Thanks for the information, but the "SwingSound" is in another script so I'm trying to call it through "mouseSoundCntrl" a link to the script "$$anonymous$$ouseSoundController" that holds the sound files.
When an Animation event fires as a result of a frame being reached, the Send$$anonymous$$essage() mechanism is utilized to find a named function (supplied as a String). This means that you need to study Send$$anonymous$$essage().
Your answer
Follow this Question
Related Questions
returning a variable in javascript 1 Answer
How to convert a 'string' to the name of a 'void' function? 1 Answer
Calling a void from another script Unexpected "String" 1 Answer
JSON Saving/loading " error CS0029: Cannot implicitly convert type 'void' to 'string' " 1 Answer
How to call a variable using a String for the variable name? 1 Answer