Search Unity

Beginner buttons

Discussion in 'Scripting' started by jtw, Jan 5, 2012.

  1. jtw

    jtw

    Joined:
    Jan 21, 2008
    Posts:
    30
    Hello people,

    I'm a 3D guy and only started looking at code yesterday so I simply lack the basics. Although reading and re-reading the documentation I just can't this very simple button to work.

    I have to make a couple of 'onmousedown' play/pause buttons to control a single animation in a scene. I understand the theory but I can't seem to wrap my head around what the actual code should look like.

    To play the animation I use this and it works fine.
    Code (csharp):
    1. var object: GameObject;
    2.  
    3. function OnMouseDown () {
    4.   object.animation.Play();
    5. }
    I get that the Pause button should simply set the speed to 0
    Code (csharp):
    1. animation[""].speed = 0;
    But what should it look like when using 'onmousedown' ?

    I know this is probably very simple but I hope you'll bear with me.


    Thanks,

    Jacob
     
  2. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    try looking into GUI.Button here on Unity's Scripting Reference and into Animation class.

    crude example:
    Code (csharp):
    1.  
    2. void OnGUI{
    3.     if(GUI.Button(new Rect(100,100,100,100), "PLAY")  !Animation.isPlaying){ //we create a new button and if no animation is playing we play the default animation, which in most cases is idle (first animation)
    4.         Animation.Play();
    5.     }
    6.     if(GUI.Button(new Rect(210,100,100,100), "STOP")  Animation.isPlaying){
    7.         Animation.Stop();
    8.     }
    9. }
    10.  
     
  3. yair1221

    yair1221

    Joined:
    Sep 28, 2011
    Posts:
    39
  4. jtw

    jtw

    Joined:
    Jan 21, 2008
    Posts:
    30
    I really appreciate the replies and I've set up the buttons using GUIbuttons instead which seems easier. But I still can't get the example to work...

    it either says "An object reference is required to access non-static member `UnityEngine.Animation.isPlaying'" ot there's no Animation attached to my game object.
     
  5. jtw

    jtw

    Joined:
    Jan 21, 2008
    Posts:
    30
    I guess the
    Code (csharp):
    1.  !Animation.isPlaying
    checks if the animation is playing? But now I can get it to play without using this.

    I only want to pause the animation and I read that the only way to do it without the animation restarting is to set animation speed to 0. So now I'm at the same point as I were before...but with nicer buttons.
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    By capitalizing Animation you're attempting to access a static member called isPlaying. isPlaying isn't static. So if the script is attached to the GameObject with the animation you'd want animation.isPlaying, otherwise you'd want yourGameObjectVar.animation.isPlaying
     
  7. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    yes, i made that up on the fly, i did not check anything to see if it works or something like that... so yes, my bad, but i warned him by saying "crude example:" :p
     
  8. jtw

    jtw

    Joined:
    Jan 21, 2008
    Posts:
    30
    That's ok, I won't learn anything by getting force fed the answers, I'm just glad someone is willing to reply at all.

    I can start and stop the animation now but I still have no idea how to set the animation speed?
     
  9. jtw

    jtw

    Joined:
    Jan 21, 2008
    Posts:
    30
    There's a good chance this is just gibberish but at least it's a pointer to what I'm trying to do.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class halfspeed : MonoBehaviour {
    5.     public Texture btnTexture1;
    6.     public GameObject test;
    7.      void OnGUI() {
    8.  
    9.         if (GUI.Button(new Rect(10, 110, 50, 50), btnTexture1))        
    10.         {
    11.         animation["test"].speed = 0;
    12.     }
    13.    
    14.     }
    15. }
    As I said I lack all the basic knowledge, but why does it say there's no animation attached to my game object?
     
  10. siflandolly

    siflandolly

    Joined:
    May 17, 2011
    Posts:
    141
    Is this script put on a gameobject as a component?

     
  11. jtw

    jtw

    Joined:
    Jan 21, 2008
    Posts:
    30
    Yes, and then I want to drag n drop my animation onto the slot on this game object like I did with the play button. (if that makes sense)
     
  12. siflandolly

    siflandolly

    Joined:
    May 17, 2011
    Posts:
    141
    The animation should already be on the gameobject. You might need to add animations to your asset in your project like in this:
    http://www.evernote.com/shard/s80/s...c0c2f7b349fa/b4868135f1c95174b68a859143686d15

    I don't think you can drag animations to a gameObject

     
  13. jtw

    jtw

    Joined:
    Jan 21, 2008
    Posts:
    30
    The exact error is : MissingComponentException: There is no 'Animation' attached to the "pause" game object, but a script is trying to access it. You probably need to add a Animation to the game object "pause". Or your script needs to check if the component is attached before using it.
     
  14. siflandolly

    siflandolly

    Joined:
    May 17, 2011
    Posts:
    141
    Is the gameObject a 3D model with animations that you created outside of Unity and imported in?
     
  15. jtw

    jtw

    Joined:
    Jan 21, 2008
    Posts:
    30
    Yes it's an imported character in fbx format.
     
  16. siflandolly

    siflandolly

    Joined:
    May 17, 2011
    Posts:
    141
    So the imported model in your Project should have an animation section like in my screenshot where you can put in the names of the animations and which frames match up to which animation. Then when you put that model in your scene as a gameObject, it should have those animations on it.

     
  17. jtw

    jtw

    Joined:
    Jan 21, 2008
    Posts:
    30
    I see what you mean and I've changed it now. No errors but nothing happens, it doesn't pause.
     
  18. jtw

    jtw

    Joined:
    Jan 21, 2008
    Posts:
    30
    Ahaa, it works now! But now there's a bunch of different problems. Anyway, thanks for the help!