Beginner buttons

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.

var object: GameObject;

function OnMouseDown () {
  object.animation.Play();
}

I get that the Pause button should simply set the speed to 0

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

try looking into GUI.Button here on Unity’s Scripting Reference and into Animation class.

crude example:

void OnGUI{
	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)
		Animation.Play();
	}
	if(GUI.Button(new Rect(210,100,100,100), "STOP")  Animation.isPlaying){
		Animation.Stop();
	}
}

i really dont know much about java, but if i were to create a button that plays or stops an animation, id use GUI buttons

edit: looks like someone got ahead of me :stuck_out_tongue:

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.

I guess the !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.

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

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:” :stuck_out_tongue:

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?

There’s a good chance this is just gibberish but at least it’s a pointer to what I’m trying to do.

using UnityEngine;
using System.Collections;

public class halfspeed : MonoBehaviour {
    public Texture btnTexture1;
	public GameObject test;
     void OnGUI() {

        if (GUI.Button(new Rect(10, 110, 50, 50), btnTexture1))        
		{
        animation["test"].speed = 0;
    }
	
	}
}

As I said I lack all the basic knowledge, but why does it say there’s no animation attached to my game object?

Is this script put on a gameobject as a component?

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)

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/sh/0ac7fc09-295a-4f3d-b2e1-c0c2f7b349fa/b4868135f1c95174b68a859143686d15

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

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.

Is the gameObject a 3D model with animations that you created outside of Unity and imported in?

Yes it’s an imported character in fbx format.

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.

I see what you mean and I’ve changed it now. No errors but nothing happens, it doesn’t pause.

Ahaa, it works now! But now there’s a bunch of different problems. Anyway, thanks for the help!