Search Unity

Using GUI button to play animation Unity 4

Discussion in 'Scripting' started by MrHolroyd, Dec 4, 2012.

  1. MrHolroyd

    MrHolroyd

    Joined:
    Nov 22, 2012
    Posts:
    14
    Im having problems getting a script to create a button then play the animation when it is pressed. So far this is what i have an im getting a console error Assets/MyScripts/SuppressorButton.js(17,1): BCE0044: expecting }, found ''. This is what i have so far the model is called Suppressor and im draging that onto the var in inspector and i added
    Code (csharp):
    1. var AttachSuppressor = Suppressor.GetComponent(Animation);
    because it wasnt finding the animation on the model. Thanks D

    Code (csharp):
    1. var Suppressor : GameObject;
    2. var AttachSuppressor = Suppressor.GetComponent(Animation);
    3.  
    4. function OnGUI()
    5.  
    6.  
    7.  
    8.     {
    9.     //Button 1
    10.     if(GUI.Button (Rect(0,0,200,100), "Suppressor"));
    11.     }
    12.    
    13.     //Once the button is clicked, do the following code.
    14.     {
    15.     Suppressor.animation.Play("AttachSuppressor", PlayMode.StopAll):
    16.     }
     
  2. Helghast

    Helghast

    Joined:
    May 7, 2012
    Posts:
    38
    Code (csharp):
    1. var Suppressor : GameObject;
    2. var AttachSuppressor : Animation;
    3.  
    4. function Start() {
    5.     AttachSuppressor = Suppressor.GetComponent(Animation);
    6. }
    7.  
    8. function OnGUI() {
    9.     //Button 1
    10.     if(GUI.Button (Rect(0,0,200,100), "Suppressor")) {
    11.         Suppressor.animation.Play("AttachSuppressor", PlayMode.StopAll):
    12.     }
    13. }
    untested, but I think this is what you're looking for :)
     
  3. MrHolroyd

    MrHolroyd

    Joined:
    Nov 22, 2012
    Posts:
    14
    Thanks im trying it now and getting this in console

    The AnimationClip 'AttachSuppressor' used by the Animation component 'Suppressor' must be marked as Legacy.
    UnityEngine.Animation:play(String, PlayMode)
    SuppressorButtonv1:OnGUI() (at Assets/MyScripts/SuppressorButtonv1.js:21)

    The animation state AttachSuppressor could not be played because it couldn't be found!
    Please attach an animation clip with the name 'AttachSuppressor' or call this function only for existing animations.
    UnityEngine.Animation:play(String, PlayMode)
    SuppressorButtonv1:OnGUI() (at Assets/MyScripts/SuppressorButtonv1.js:21)

    This is how my inspector of the gameobject.

     
  4. Helghast

    Helghast

    Joined:
    May 7, 2012
    Posts:
    38
    In that case it's as easy as calling animation.Play from your script :)

    Code (csharp):
    1. function OnGUI() {
    2.     //Button 1
    3.     if(GUI.Button (Rect(0,0,200,100), "Suppressor")) {
    4.         animation.Play("AttachSuppressor", PlayMode.StopAll):
    5.     }
    6. }
    That's all you need then (no GameObject references etc)
     
  5. MrHolroyd

    MrHolroyd

    Joined:
    Nov 22, 2012
    Posts:
    14
    Ive just tried that but i am still getting the same error code but on line 4, it dosnt look like it is finding the animation, or i have the settings wrong on the animation some where.
     
  6. Helghast

    Helghast

    Joined:
    May 7, 2012
    Posts:
    38
    This Error was pretty clear: "The AnimationClip 'AttachSuppressor' used by the Animation component 'Suppressor' must be marked as Legacy."

    On the asset you're importing, select the "Rig" tab, and set "Animation Type" to "Legacy", hope that helps :)
     
  7. MrHolroyd

    MrHolroyd

    Joined:
    Nov 22, 2012
    Posts:
    14
    Thanks ive got that working great. I also want a button to play the animation on the first press and then on the second play the animation in reverse. This is what i have so far but having some problems.

    Code (csharp):
    1. var Attachment : GameObject;
    2.  
    3.  function OnGUI()
    4.  {
    5.  
    6.     //Button 1
    7.  
    8.     if(GUI.Button (Rect(1060,0,200,100), "Explode View"))
    9.    
    10.     {
    11.             if (animation.time) = 0.0);
    12.             {            
    13.                 animation.Play();
    14.             }
    15.     }
    16.     {
    17.  
    18.             if (animation.time < 0.0);
    19.             {
    20.                 animatio.Play();
    21.                 animation.speed = -1.0;
    22.             }
    23.      
    24.        
    25.     }
    26. }