Noob here. I have three animations set up in the animator. But I can’t seem to wrap my head around how to assign each animations to three set of buttons in the script. I think I am to use Button.OnClick() and animator.Play(“Animation name”) but I don’t even how to begin with the script. Can someone give an example of script for that?
Pretty much any button tutorial for Unity will cover that.
Review animations too… if you’re using an Animator, usually you set a property to trigger a state change to a new state, but there’s other ways to go about it as well.
I did follow through the tutorial and wrote a code as below:
public class Button1Behavior : MonoBehaviour
{
Animator animator;
public void OnButtonPress()
{
animator.SetTrigger(“Walk”);
}
}
And then, I attached this script to the GameObject and turned on the OnClick() function in Button’s Inspector. I also added the trigger condition which does change the animations but the button doesn’t work at all upon clicking on it. Do note that I am an absolute noob at scripting.
Always use code tags when pasting code snippets.
Well then, starting with the basics would be a bright idea. Learn how to use unity, and how to make a game
https://www.youtube.com/watch?v=IlKaB1etrik
https://www.youtube.com/watch?v=Lu76c85LhGY
EDIT: Answering your question, read this
https://docs.unity3d.com/ScriptReference/Animator.SetTrigger.html
Did you add the animator to your GameObject? Did you assign it your controller?
Hey, there. I actually got the right codes to work. And the buttons assigned are working great but now, I got a code efficiency problem. The following lines of code worked:
public class ButtonsBehavior : MonoBehaviour
{
public Animator anim;
public Animator anim2;
public void PlayAnimation()
{
anim.Play(“Animation01”);
}
public void PlayAnimation2()
{
anim.Play(“Animation02”);
}
}
But obviously, if I have say 15 different sets of animations, it wouldn’t be efficient to repeat the codes above for each 15 sets. So, how would I go about it to make it more concise?
I’m pretty sure I said to use code tags when pasting code snippets, read this ( https://discussions.unity.com/t/481379 )
Well, I assume these are 15 animations are for different buttons and/or characters, if so you’ll just have to repeat it for each button and/or character.
Looping over a collection of some kind is one way. Again, start with some tutorials on collections in C# and how to interoperate with collections of items in the Unity editor.
And use code tags.
When you write code and it doesn’t work, you must find a way to get the information you need in order to reason about what the problem is.
What is often happening in these cases is one of the following:
- the code you think is executing is not actually executing at all
- the code is executing far EARLIER or LATER than you think
- the code is executing far LESS OFTEN than you think
- the code is executing far MORE OFTEN than you think
- the code is executing on another GameObject than you think it is
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
- is this code even running? which parts are running? how often does it run? what order does it run in?
- what are the values of the variables involved? Are they initialized? Are the values reasonable?
- are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)
Knowing this information will help you reason about the behavior you are seeing.
You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong: