I’m not getting wheel the first animation of my game

Asked

Viewed 111 times

2

Good afternoon I’m having a hard time understanding what that mistake would be I still have a few days in the programming world someone can help me

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {
    public float Velocidade;
    public Transform player;
    private Animator animator;



    // Use this for initialization
    void Start () {
        animator = player.GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update () {
        Movimentar();
    }
    void Movimentar ()
    {
        animator.SetFloat ("Run", Mathf.Abs (Input.GetAxis ("Horizontal")));

    if (Input.GetAxisRaw ("Horizontal") > 0) {
            transform.Translate (Vector2.right * Velocidade * Time.deltaTime);
            transform.eulerAngles = new Vector2 (0,0);
        }

            if(Input.GetAxisRaw ("Horizontal") < 0) {

            transform.Translate(Vector2.right * Velocidade * Time.deltaTime);
                transform.eulerAngles = new Vector2(0,180);
            }
        }
}

the error after trying executes the project

Nullreferenceexception: Object Reference not set to an instance of an Object Player.Move () (at Assets/Scripts/Player.Cs:22) Player.Update() (at Assets/Scripts/Player.Cs:18)

Unassignedreferenceexception: The variable player of Player has not been Assigned. You probably need to assign the player variable of the Player script in the inspector. Unityengine.Component.Getcomponent[Animator] () (at C:/buildslave/Unity/build/artifacts/generated/common/Runtime/Unityenginecomponent.gen.Cs:45) Player.Start() (at Assets/Scripts/Player.Cs:13)

2 answers

1

The error is in your project and not in the code.

He is complaining that you have not assigned any value to the variable player. Within your project, drag the Asset you want to grab the property animatorup to the variable player.

0

You declared the variable Player? In the first line you could declare as a New Transform.
In your code it’s like this:

public Transform player;

Replace with this:

public Transform player = new Transform;

I hope it works... hugs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.