How to start using the button?

I have an object.

After adding the script, I can change the animation layer using the Space button:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class JustPlayer : MonoBehaviour {

	public int forceFly = 100;
	private Animator myAnimator;
	private Rigidbody2D rb;


	public bool shield = false;

	// Use this for initialization
	void Start () {
    
		rb = GetComponent<Rigidbody2D> ();
		myAnimator = GetComponent<Animator> ();
	}

   
	// Update is called once per frame
	void FixedUpdate () {


		if (Input.GetMouseButtonDown (0)) {
			rb.velocity = Vector2.zero;
			rb.AddForce (Vector2.down * forceFly);
			myAnimator.SetTrigger ("Swim");
        		}

		if (Input.GetKeyDown (KeyCode.Space)) {
			
			StartCoroutine (BubbleSwim ());
			     
		}
	}
        
	void AnimationControl () {

		if (shield == false) {
			myAnimator.SetLayerWeight (1, 0);

		}
		else{
			myAnimator.SetLayerWeight (1, 1);
    
	}
}

	IEnumerator BubbleSwim ()
	{
		myAnimator.SetLayerWeight (1, 1);
		yield return new WaitForSeconds (10f);
		myAnimator.SetLayerWeight (1, 0);
}
}

I would like to change the animation layer using a button that I will add to the screen.

How to do it?

Hi, create a new function in your called for example “StartCoroutine” like this:

    public void StartCoroutine()
    {
        StartCoroutine(BubbleSwim());
    }

Then create a UI Button and in the inspector go to the Button component where there is written “On Click ()”. Press the + button and then drag and drop the GameObject with the script in the “None (Object)” field. Press the “No Function” and choose “JustPlayer” and then “StartCoroutine ()”.
Now when you click the button it will call the “StartCoroutine ()” function.