0

我收到错误 NullReferenceException: Object reference not set to an instance of an object ThirdPersonCamera.Update () (at Assets/scripts/ThirdPersonCamera.cs:24)

我的代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
using UnityEngine.SocialPlatforms;
using UnityEngine.UI;
using UnityStandardAssets.Utility;

public class ThirdPersonCamera : MonoBehaviour {

    [SerializeField]Vector3 cameraOffset;
    [SerializeField]float damping;

    Transform cameraLookTarget;
    Player localPlayer;

    void Awake () {
        GameManger.Instance.OnLocalPlayerJoined += HandleOnLocalPlayerJoined;
    } 

    void HandleOnLocalPlayerJoined (Player player) {
        localPlayer = player;
        cameraLookTarget = localPlayer.transform.Find("cameraLookTarget");

        if (cameraLookTarget == null) {
            cameraLookTarget = localPlayer.transform;
        }
    }


    // Update is called once per frame
    void Update () {
        Vector3 targetPosition = cameraLookTarget.position + localPlayer.transform.forward * cameraOffset.z +
                localPlayer.transform.up * cameraOffset.y +
                localPlayer.transform.right * cameraOffset.x;

        transform.position = Vector3.Lerp(transform.position, targetPosition, damping * Time.deltaTime);
    }
}

我尝试更改脚本执行顺序,但没有任何效果。我不知道怎么了。

4

1 回答 1

1

确保在脚本中将 GameObject 分配给 LocalPlayer 变量。该对象正在您的层次结构中查找不带引号的名为“cameraLookTarget”的内容。资本化问题。

我建议在您的 Awake() 方法中查找 LocalPlayer 对象,如果它为空,请使用 Debug.Log("No local player assigned") 来提醒自己这实际上没有被分配。

于 2017-04-20T18:54:55.623 回答