我收到错误NullReferenceException:对象引用未设置为对象的实例 ThirdPersonCamera.Update()(在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);
}
}
我尝试过更改脚本执行顺序,但没有任何作用。我不知道出了什么问题。
答案 0 :(得分:1)
确保在脚本中为GamePlayer变量分配了GameObject。此对象在您的层次结构中查找名为' cameraLookTarget'没有引号。资本化很重要。
我建议在Awake()方法中查找LocalPlayer对象,如果它为null,则使用Debug.Log("没有指定本地播放器")提醒自己这实际上没有被分配。