Play animation once? - c#

I've made a simple animation for my dishwashing machine in 3dsmax. I imported the object as an fbx file into Unity. The animation is called "openandclose". The animation itself opens and closes the machine.
However I can't seem to find a way to trigger the animation to happen once at the click of a button. How can I do this?

Unity has a great documentation regarding every core function including animations. Check this link out: http://docs.unity3d.com/462/Documentation/ScriptReference/Animation.Play.html
and to get the button pressed:
http://docs.unity3d.com/462/Documentation/ScriptReference/Input.GetKeyDown.html
combined it would be something like this i guess:
void Update() {
if (Input.GetKeyDown("e"))
animation.Play("openandclose");
}

Related

The animation state .. could not be played because it couldn't be found?

I am trying to start an animation by pressing a key on my keyboard.
For this I am using the following code:
[HideInInspector] public Animation animation;
private void Start()
{
animation = GetComponent<Animation>();
}
private void Update()
{
if (Input.GetKeyDown("1"))
{
animation.Play("AnimationName");
}
}
This gives me the following error:
The animation state AnimationName could not be played because it
couldn't be found!
Although, the animation DOES start, but how can I clear this error?
I think the only possible explaination is that your animation clip is not marked as Legacy.
https://www.unity3dtips.com/zh/the-animation-state-could-not-be-played-because-it-couldnt-be-found/
EDIT with step by step here:
Animations must be marked as legacy to be used with the animation component.
In the project window select the animation clips you’re trying to play.
Set the inspector to debug mode which exposes hidden variables, in this case it’ll make the “Legacy” checkbox appear.Unity Inspector Debug Mode
Tick the “Legacy” checkbox and change the inspector back to normal mode.
Unity Inspector Animation Clip Legacy Checkbox
But there is one thing you should know: you should avoid, if possible, the old legacy way to play animation.
Learn how to use an Animator component. Just create an animator, assign your animation clip, decide a boolean for start the animation in the animator component and set it to true in your code (for basic use).
Sure there is some things to do beside code but it will be easier to control your animation, bleand different animations and change them to your needs (it's for example much easier to loop or stop the animation at some point).
It would be also easier to understand when it start and when is playing.
Another thing to check is the legacy 'Animation' component on your gameObject that is supposed to perform the animation. In my case things were marked as legacy for the animation on the imported animation but somehow when adjusting settings on my import it dropped the animation it was complaining about from the actual 'Animation' component so once I added it things worked as expected
Inspector in Debug mode showed my animation marked as 'Legacy' true
My Animation component showed the missing animation

Button in unity doesn't work, how can i fix it?

I am new to unity and I am still learning. I am making a game and its going great but I have a problem with a Quit button.
It doesn't use the script attached to the On Click () section in the button menu.
There's an EventSystem in the Hierarchy, the Force Module Active is checked, the button is a child to a canvas. I don't know why the button isn't working
Here is the code in the script attached to On Click ()
public void Quit()
{
Debug.Log ("YES IT WORKS");
Application.Quit ();
}
I want the program to close when the button is pressed but when the button is pressed nothing happens!
The function is not being called when the button is pressed which means that you have not selected the function in the onClick settings. To do this, ensure that the script is attached to the button and then dragged into the onClick box of the button in the inspector. Next, click on the dropdown and hover over the name of the attached script and select the function that you want to call.
Solution
Instead of using Application.Quit() try to shift the your working scene to the home screen .Your every game should start and end at the same level which is also known as the main scene of the the game. Secondly before shifting to other scene you should destroy your player and save the scores in the player pref .
Player Pref:
The library which helps the user to store the data and show them on the main screen , Hence the use of database will not be there in order to save the data for the small games . You are beginner so you should look at these all thing on You tube or the unity website documentation available on its website
Are you in the Editor?
Application.Quit() only works when you build the application (for PC and Android. It should not work on iOS, and even if it works it should display as a crash so it's not good).

How to show cursor in Unity "Play mode"

I am new to Unity, and have created a simple start menu with a play button that will load a game scene. However, when I click "Play" in Unity to test my game, I do not have a cursor. I have to hit escape for my cursor to appear. Do I need to create a script file to make my cursor visible, or how do I change this? I am unable to find anything in Unity forums. Googling the topic retrieves many "How to hide cursor" threads.
I have tried adding an empty object and adding a c# script to it that contains the following but I think I am missing a step?
public class CursorBehavior : MonoBehaviour {
// Use this for initialization
void Start () {
Cursor.visible=true;
}
}
I think this can help you.
Cursor.visible

Unity - animation is not playing even thought animation.IsPlaying() is already returning 'true'

I tried playing attack animation on my 3d model using Animation.play("Attack") but somehow the animation is not playing, i already checked using Animation.IsPlaying("Attack") and it returned true, so i don't think it's the coding. Here is the code by the way:
animation.Play("Attack");
if(animation.IsPlaying("Attack")){
Debug.Log("Is Attacking");
}
I'm guessing it has something to do with the animation setting but i don't know what, anyone can help?
Assuming you are using the built-in Animation component, open the Inspector and make sure that the "Attack" animation (or any other animations) is properly added under the Animation component.
Refer to: Unity Docs - Animation

Unity; animation on prefab

Tried to find this kind of problem around the net but failed so...
Here's the thing - I have a prefab gameobject that is to represent a unit, portrait more specifically. It has several scripts attached and an Animation component with two animations: Static and Selected.
The prefab is instantiated, moved freely and, after placing, it can be clicked to select it, which should, aside from executing a bit of code, start the Selected animation.
Using this code:
void OnMouseDown(){
//
//Some inside stuff
//
if (this.GetComponent<UnitHandling> ().thisUnit.Selected)
this.animation.Play("Selected");
if(this.animation.IsPlaying ("Selected"))
Debug.Log("Animation of selection is playing");
}
I checked that the animation seems to be playing (at least the Debug message is showing) but I see no animation... What am I doing wrong?
Try making an animation state using mechanim, and play it using this:
GetComponent<Animator>().CrossFade("Selected", 0);
https://docs.unity3d.com/Documentation/ScriptReference/Animator.CrossFade.html
https://docs.unity3d.com/Documentation/Manual/MecanimAnimationSystem.html

Categories

Resources