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 /
  • Help Room /
avatar image
0
Question by Gerom_Geronimo · Apr 13, 2016 at 05:56 PM · c#animationscripting problemclick

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

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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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);
     }
 }
 
Comment
Add comment · Show 7 · 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 Gerom_Geronimo · Apr 13, 2016 at 07:18 PM 0
Share

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

avatar image TBruce Gerom_Geronimo · Apr 13, 2016 at 10:59 PM 1
Share

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

avatar image Gerom_Geronimo · Apr 14, 2016 at 10:54 PM 0
Share

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.

avatar image Gerom_Geronimo · Apr 14, 2016 at 11:11 PM 0
Share

Never$$anonymous$$d! It's must be just "string" and it works! Super thank you

avatar image TBruce Gerom_Geronimo · Apr 15, 2016 at 12:37 AM 0
Share

$$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.

avatar image Nikitachan · Feb 28, 2017 at 03:45 PM 0
Share

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.alt text

unity1.jpg (61.9 kB)
avatar image TBruce Nikitachan · Mar 02, 2017 at 03:52 AM 0
Share

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

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

165 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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