Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Unity 5第三人称脚本错误_C#_Unity3d_Frame Rate_Tps - Fatal编程技术网

C# Unity 5第三人称脚本错误

C# Unity 5第三人称脚本错误,c#,unity3d,frame-rate,tps,C#,Unity3d,Frame Rate,Tps,我得到的错误是NullReferenceException:对象引用未设置为对象的实例 更新地址:Assets/scripts/ThirdPersonCamera.cs:24 我的代码: using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Runtime.InteropServices; using UnityEngine.SocialPlatforms; usin

我得到的错误是NullReferenceException:对象引用未设置为对象的实例 更新地址: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);
    }
}

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

确保在脚本中为LocalPlayer变量分配了一个游戏对象。此对象正在您的层次结构中查找名为“cameraLookTarget”的对象,但不带引号。资本化很重要

我建议您在Awake方法中查找LocalPlayer对象,如果该对象为null,请使用Debug.LogNo local player assigned来提醒您自己实际上没有分配该对象