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.
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 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:”
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?
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.
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.