unity 出现如下报错,求解决

运行报错
NullReferenceException: Object reference not set to an instance of an object Player.Update () (at Assets/Scripts/Player.cs:49)

相关代码


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GameManager : MonoBehaviour 
{
    public static GameManager Instance = null;
    int m_score = 0;
    static int m_hisScore = 0;
    int m_ammo = 30;
    Player m_player;
    Text txt_ammo;
    Text txt_hisScore;
    Text txt_life;
    Text txt_Score;

    

    // Use this for initialization
    void Start () {
        Instance = this;
        m_player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
        foreach (Transform t in this.transform.GetComponentInChildren<Transform>())
        {
            if (t.name.CompareTo("txt_ammo") == 0)
            {
                txt_ammo = t.GetComponent<Text>();
            }
            else if (t.name.CompareTo("txt_hisScore") == 0)
            {
                txt_hisScore = t.GetComponent<Text>();
                txt_Score.text = "High Score" + m_hisScore;
            }
            else if (t.name.CompareTo("txt_life") == 0)
            {
                txt_life = t.GetComponent<Text>();
            }
            else if (t.name.CompareTo("txt_Score") == 0)
            {
                txt_Score = t.GetComponent<Text>();
            }
        }
    }

    public void SetScore(int score)
    {
        m_score += score;
        if (m_score > m_hisScore)
            m_score = m_hisScore;
        txt_Score.text = "Score<color=yellow>" + m_score + "</color>";
        txt_hisScore.text = "HighScore" + m_hisScore;
    }

    public void SetAmmo(int ammo)
    {
        m_ammo -= ammo;
        if (m_ammo <= 0)
            m_ammo = 30 - m_ammo;
        txt_ammo.text = m_ammo.ToString() + "/30";
    }

    public void SetLife(int life)
    {
        txt_life.text = life.ToString();
    }

    void OnGUI()
    {
        if (m_player.m_life <= 0)
        {
            GUI.skin.label.alignment = TextAnchor.MiddleCenter;
            GUI.skin.label.fontSize = 40;
            GUI.Label(new Rect(0, 0, Screen.width, Screen.height), "Game Over");
            GUI.skin.label.fontSize = 30;
            if (GUI.Button(new Rect(Screen.width * 0.5f - 150, Screen.height * 0.75f, 300, 40), "Try again"))
            {
                SceneManager.LoadScene("main");
                //Application.LoadLevel(Application.loadedLevelName);
            }
        }
    }
    // Update is called once per frame
    void Update () {
        
    }
}

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

public class Player : MonoBehaviour 
{

    public Transform m_transform;
    CharacterController m_ch;
    float m_movSpeed = 5.0f;
    float m_gravity = 6.0f;
    public int m_life = 5;
    public float m_jumpSpeed = 10.0f;
    private Vector3 m_movDirection = Vector3.zero;
    Transform m_camTransform;
    Vector3 m_canRot;
    float m_camHeight = 1.4f;
    Transform m_muzzlepoint;
    public LayerMask m_layer;
    public Transform m_fx;
    public AudioClip m_audio;
    float m_shootTimer = 0;

    void Start()
    {
        m_transform = this.transform;
        m_ch = this.GetComponent<CharacterController>();
        m_camTransform = Camera.main.transform;
        Vector3 pos = m_transform.position;
        pos.y += m_camHeight;
        m_camTransform.position = pos;
        m_camTransform.rotation = m_transform.rotation;
        m_canRot = m_camTransform.eulerAngles;
        Cursor.visible = true;
        m_muzzlepoint = m_camTransform.Find("weapon/muzzlepoint").transform;
    }
    void Update()
    {
        if (m_life <= 0)
            return;
        Control();
        m_shootTimer -= Time.deltaTime;
        if (Input.GetMouseButton(0) && m_shootTimer <= 0)
        {
            m_shootTimer = 0.1F;
            this.GetComponent<AudioSource>().PlayOneShot(m_audio);
            //this.audio.PlyerOneShot(m_audio);
            GameManager.Instance.SetAmmo(1);
            RaycastHit info;
            bool hit = Physics.Raycast(m_muzzlepoint.position, m_camTransform.TransformDirection(Vector3.forward), out info, 100, m_layer);
            if (hit)
            {
                if (info.transform.tag.CompareTo("enemy") == 0)
                {
                    Enemy enemy = info.transform.GetComponent<Enemy>();
                    enemy.OnDamage(1);
                }
                Instantiate(m_fx, info.point, info.transform.rotation);
            }
        }
    }
    void Control()
    {
        float rh = Input.GetAxis("Mouse X");
        float rv = Input.GetAxis("Mouse Y");
        m_canRot.x -= rv;
        m_canRot.y += rh;
        m_camTransform.eulerAngles = m_canRot;

        Vector3 camRot = m_camTransform.eulerAngles;
        camRot.x = 0;
        camRot.z = 0;
        m_transform.eulerAngles = camRot;
        Vector3 pos = m_transform.position;
        pos.y += m_camHeight;
        m_camTransform.position = pos;

        float xm = 0, ym = 0, zm = 0;
        ym -= m_gravity * Time.deltaTime;
        if (Input.GetKey(KeyCode.W))
        {
            zm += m_movSpeed * Time.deltaTime;
        }
        else if (Input.GetKey(KeyCode.S))
        {
            zm -= m_movSpeed * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.A))
        {
            xm -= m_movSpeed * Time.deltaTime;
        }
        else if (Input.GetKey(KeyCode.D))
        {
            xm += m_movSpeed * Time.deltaTime;
        }
        if (Input.GetKeyDown(KeyCode.Space))
        {
            m_movDirection.y = m_jumpSpeed;
        }
        m_movDirection.y -= m_gravity * Time.deltaTime;
        m_ch.Move(m_transform.TransformDirection(new Vector3(xm, ym, zm)));
        m_ch.Move(m_movDirection * Time.deltaTime);
    }
    public void OnDamage(int damage)
    {
        m_life -= damage;
        GameManager.Instance.SetLife(m_life);
        if (m_life <= 0)
        {
            Cursor.visible = false;
            //Screen.lockCursor = false;
        }
    }
}

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

public class Enemy : MonoBehaviour {

    Transform m_transform;
    Animator m_ani;
    Player m_player;
    NavMeshAgent m_agent;
    float m_movSpeed = 2.5f;
    float m_rotSpeed = 30;
    float m_timer = 2;
    int m_life = 15;
    protected EnemySpawn m_spawn;

    void Start()
    {
        m_transform = this.transform;
        m_player = GameObject.FindGameObjectWithTag("Player").GetComponent<Player>();
        m_ani = this.GetComponent<Animator>();
        m_agent = GetComponent<NavMeshAgent>();
        m_agent.speed = m_movSpeed;
        m_agent.SetDestination(m_player.m_transform.position);
    }

    void Update()
    {
        if (m_player.m_life <= 0)
            return;
        AnimatorStateInfo stateInfo = m_ani.GetCurrentAnimatorStateInfo(0);
        if (stateInfo.fullPathHash == Animator.StringToHash("Base Layer.idle") && !m_ani.IsInTransition(0))
        {
            m_ani.SetBool("idle", false);
            m_timer -= Time.deltaTime;
            if (m_timer > 0)
                return;
            if (Vector3.Distance(m_transform.position, m_player.m_transform.position) < 1.5f)
            {
                m_ani.SetBool("attack", true);
            }
            else
            {
                m_timer = 1;
                m_agent.SetDestination(m_player.m_transform.position);
                m_ani.SetBool("run", true);
            }
        }
        if (stateInfo.fullPathHash == Animator.StringToHash("Base Layer.run") && !m_ani.IsInTransition(0))
        {
            m_ani.SetBool("run", false);
            m_timer -= Time.deltaTime;
            if (m_timer < 0)
            {
                m_agent.SetDestination(m_player.m_transform.position);
                m_timer = 1;
            }
            if (Vector3.Distance(m_transform.position, m_player.m_transform.position) <= 1.5f)
            {
                m_agent.isStopped = true;
                //m_agent.Stop();
                m_ani.SetBool("attack", true);
            }
        }
        if (stateInfo.fullPathHash == Animator.StringToHash("Base Layer.attack") && !m_ani.IsInTransition(0))
        {
            RotateTo();
            m_ani.SetBool("attack", false);
            if (stateInfo.normalizedTime >= 1.0f)
            {
                m_ani.SetBool("idle",true);
                m_timer = 2;
                m_player.OnDamage(1);
            }
        }
        if (stateInfo.fullPathHash == Animator.StringToHash("Base Layer.death") && !m_ani.IsInTransition(0))
        {
            if (stateInfo.normalizedTime >= 1.0f)
            {
                m_spawn.m_enemyCount--;
                GameManager.Instance.SetScore(100);
                Destroy(this.gameObject);
            }
        }
    }

    void RotateTo()
    {
        Vector3 targetdir = m_player.m_transform.position - m_transform.position;
        Vector3 newDir = Vector3.RotateTowards(transform.forward, targetdir, m_rotSpeed * Time.deltaTime,0.0f);
        m_transform.rotation = Quaternion.LookRotation(newDir);
    }

    public void OnDamage(int damage)
    {
        m_life -= damage;
        if (m_life <= 0)
        {
            m_ani.SetBool("death", true);
        }
    }

    public void Init(EnemySpawn spawn)
    {
        m_spawn = spawn;
        m_spawn.m_enemyCount++;
    }
}

img


是这行吗?

img


你在这debug一下看是不是没获取到

解决了吗?我跟你是同一个问题,而且代码都一样