如何在 Unity 中修复 NullReferenceException?无法通过鼠标左键单击接近玩家与对象交互

问题描述 投票:0回答:0

我正在按照 Info Gamer 制作 Among Us 游戏的教程进行操作。我无法通过单击鼠标左键与玩家附近的对象进行交互。

教程的网站是https://www.infogamerhub.com/among-us-interactable-objects/.

有两个错误。 第一个错误是“

NullReferenceException
:对象引用未设置为对象的实例

RS_Player_Controller.Interact 
(UnityEngine.InputSystem.InputAction+CallbackContext context) (at 
Assets/Scripts/RS_Player_Controller.cs:111)"

第二个错误是:

"NullReferenceException while executing 'performed' callbacks of 
'INTERACTION[/Mouse/leftButton]'"

RS_Player_Controller
的代码及相关代码如下。我认为错误是在
RS_Player_Controller
中进行交互,但无法修复错误。

RS_Player_Controller

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


public class RS_Player_Controller : MonoBehaviour
{
    [SerializeField] bool hasControl;
    public static RS_Player_Controller localPlayer;

    //Components
    Rigidbody myRB;
    Animator myAnim;
    Transform myAvatar;
    //Player movement
    [SerializeField] InputAction WASD;
    Vector2 movementInput;
    [SerializeField] float movementSpeed;
    //Player Color
    static Color myColor;
    SpriteRenderer myAvatarSprite;

    //Interaction
    [SerializeField] InputAction MOUSE;
    Vector2 mousePositionInput;
    Camera myCamera;
    [SerializeField] InputAction INTERACTION;
    [SerializeField] LayerMask interactLayer;

    private void Awake()
    {
        INTERACTION.performed += Interact;
    }



    private void OnEnable()
    {
        WASD.Enable();
        MOUSE.Enable();
        INTERACTION.Enable();
    }

    private void OnDisable()
    {
        WASD.Disable();
        MOUSE.Disable();
        INTERACTION.Disable();
    }



    // Start is called before the first frame update
    void Start()
    {
        if(hasControl)
        {
            localPlayer = this;
        }

        myRB = GetComponent<Rigidbody>();
        myAnim = GetComponent<Animator>();
        myAvatar = transform.GetChild(0);
        myAvatarSprite = myAvatar.GetComponent<SpriteRenderer>();
        if (!hasControl)
            return;
        if (myColor == Color.clear)
            myColor = Color.white;
        myAvatarSprite.color = myColor;

    }

    // Update is called once per frame
    void Update()
    {
        if (!hasControl)
            return;

        movementInput = WASD.ReadValue<Vector2>();
        myAnim.SetFloat("Speed", movementInput.magnitude);
        if (movementInput.x != 0)
        {
            myAvatar.localScale = new Vector2(Mathf.Sign(movementInput.x), 1);
        }

        mousePositionInput = MOUSE.ReadValue<Vector2>();

    }

    private void FixedUpdate()
    {
        myRB.velocity = movementInput * movementSpeed;
    }

    public void SetColor(Color newColor)
    {
        myColor = newColor;
        if (myAvatarSprite != null)
        {
            myAvatarSprite.color = myColor;
        }
    }

    void Interact(InputAction.CallbackContext context)
    {
        if (context.phase == InputActionPhase.Performed)
        {
            //Debug.Log("Here");
            RaycastHit hit;
            Ray ray = myCamera.ScreenPointToRay(mousePositionInput);
            if (Physics.Raycast(ray, out hit,interactLayer))
            {
                if (hit.transform.tag == "Interactable")
                {
                    if (!hit.transform.GetChild(0).gameObject.activeInHierarchy)
                        return;
                    RS_Interactable temp = hit.transform.GetComponent<RS_Interactable>();
                    temp.PlayMiniGame();
                }
            }
        } 
    }


}

RS_Interactable

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

public class RS_Interactable : MonoBehaviour
{
    [SerializeField] GameObject miniGame;
    GameObject highlight;

    private void OnEnable()
    {
        highlight = transform.GetChild(0).gameObject;
    }

    private void OnTriggerEnter(Collider other)
    {
        if(other.tag == "Player")
        {
            highlight.SetActive(true);
        }
    }

    private void OnTriggerExit(Collider other)
    {
        if(other.tag == "Player")
        {
            highlight.SetActive(false);
        }
    }

    public void PlayMiniGame()
    {
        miniGame.SetActive(true);
    }
}
c# unity3d input raycasting interaction
© www.soinside.com 2019 - 2024. All rights reserved.