Click on object and play animation\sound C# script
Hi, guys, can you help with that:
public class TriggerScript : MonoBehaviour {
public GameObject object1;
public Animator ObjectAnimator;
public Animation ObjectAnimation;
public AudioClip ObjectAudio;
void Start () {
}
void Update () {
if (Input.GetMouseButtonDown (0))
object1.GetComponent<Animation>().Play() = ObjectAnimation;
GetComponent<AudioSource> ().Play () = ObjectAudio;
}
}
I just want script when player click on object and animation of this object is start playing (sound too, of course). It's 2D project. C# please
I'm very beginner. Scripting just like that, sort out so many variants, tired of this didn't work
Answer by TBruce · Apr 13, 2016 at 06:13 PM
@TriggerScript Check out the following public GameObject object1; public Animator ObjectAnimator; public Animation ObjectAnimation; public AudioClip ObjectAudio;
private AudioSource audioSource;
private Animation animation;
void Start ()
{
// make sure that we have an AudioSource - do this here once instead of every frame
if (audioSource == null)
{ // if AudioSource is missing
Debug.LogWarning("AudioSource component missing from this gameobject. Adding one.");
// let's just add the AudioSource component dynamically
audioSource = gameObject.AddComponent<AudioSource>();
}
// make sure that object1 is a valid GameObject and has an animation - do this here once instead of every frame
if ((object1 != null) && (object1.GetComponent<Animation>() != null))
{
animation = object1.GetComponent<Animation>();
}
}
void Update ()
{
if ((Input.GetMouseButtonDown (0)) && (object1 != null))
{
animation.Play();
audioSource.PlayOneShot(coinSFX);
}
}
Thanks very much, but now that animation I'd want to add is my headache. "Animation component must be marked as Legacy" "Default clip could not be found in attached animations list" argh
Yes, legacy animations are not the way to go in Unity 5.
One way to do this is to open the Animator with the object that has the animation selected (in this case object1) under the "Window\Animator" in the main menu.
First create a trigger by selecting the "Parameters" tab in the Animator window. Next type in a name for the trigger (something appropriate) and press the "+" button to the right. Now to need to create an Idle state. To do this right click in the Base Layer window and select Create State/Empty. Then select the new state and rename it to Idle. Next right click on the Idle state and select "Set as Layer Default State".
Next right click on the idle state and select $$anonymous$$ake Transition. Drag that over to the animation state that you want to animate and click it (in the example image below it is a Sphere). Now click on the transition that you just created and over on the left side you will see a Conditions window. Click the "+" button and your trigger that you created earlier should appear immediately if not select it from the drop down box.
If you are not looping the animation but you do want the animation to play the next time you select the object, you will need to create a transition back to the Idle state just like you did before but this time select you animation state object (the Sphere as seen below) and select $$anonymous$$ake Transition dragging it to the Idle state. You need do nothing more in the animator.
Here is a link to the image that I was referring to, for some reason it could not be uploaded.
Here is updated script using the animator
public GameObject object1;
public AudioClip audioClip;
public String animationTrigger;
private AudioSource audioSource;
private Animator animator;
void Start ()
{
// make sure that we have an AudioSource - do this here once ins$$anonymous$$d of every frame
if (audioSource == null)
{ // if AudioSource is missing
Debug.LogWarning("AudioSource component missing from this gameobject. Adding one.");
// let's just add the AudioSource component dynamically
audioSource = gameObject.AddComponent<AudioSource>();
}
// make sure that object1 is a valid GameObject and has an animation - do this here once ins$$anonymous$$d of every frame
if ((object1 != null) && (object1.GetComponent<Animator>() != null))
{
animator = object1.GetComponent<Animator>();
}
}
void Update ()
{
if ((Input.Get$$anonymous$$ouseButtonDown (0)))
{
if ((animator != null) && (animationTrigger != ""))
animator.SetTrigger(animationTrigger);
audioSource.PlayOneShot(audioClip);
}
}
This is even more than I hoped for, but what is "public String"? Here a error message in $$anonymous$$ono: "The name 'String' does not exists in the current context". I feel so helpless because of asking every time.
Never$$anonymous$$d! It's must be just "string" and it works! Super thank you
$$anonymous$$y bad, "string" is the default string class that is defined in Unity. To use the "String" class type you need to use the System library like so "using System;". I am used to using the latter version and some of the times I miss type it when coding for someone else.
Hi @TBruce, i am doing similar project. I tried ur latest code in this post but only the audio is played when i clicked on the object. The animation does not play. i had also set the parameter in the animator. Your reply is kindly appreciated.
You mention the audio is playing but the animation is not. Not sure what the problem is by a screen shot. But the first thing I would do is verify the the animation controller and trigger when pressing the mouse button. Try changing the code to this as a test
void Update ()
{
if ((Input.Get$$anonymous$$ouseButtonDown (0)))
{
if ((animator != null) && (animationTrigger != ""))
{
animator.SetTrigger(animationTrigger);
audioSource.PlayOneShot(audioClip);
}
}
}
If you no longer hear audio then either your controller or trigger is invalid.
I do not know how your GameObject is set up, but one thing you need to watch out for when assigning variables (especially if they are public), if you blatantly assign a non null variable it is very possible that the variable can become null and unusable. So if you followed the code in Start() as seen below
if ((object1 != null) && (object1.GetComponent<Animator>() != null))
{
animator = object1.GetComponent<Animator>();
}
It could be possible that you are reassigning a variable that you do not need to (as I said, I do not know how your code is set up).
Good Luck!
Your answer
Follow this Question
Related Questions
How to have a trigger activate animation on another game object. 2 Answers
Animation rigging doesnt work until I disable and re-enable Rig Builder 2 Answers
Help with OnTriggerEnter Not working correctly? 1 Answer
Animation/Movement Error 1 Answer
What reason could there be, if a parent object refuses to take the new given position? 0 Answers