2017-04-20 37 views
0

我得到了錯誤的NullReferenceException:未將對象引用設置到對象 ThirdPersonCamera.Update()的一個實例(在資產/腳本/ ThirdPersonCamera.cs:24)團結5第三人腳本錯誤

我代碼:

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); 
    } 
} 

我試過更改腳本執行順序,但沒有任何工作。我不知道什麼是錯的。

回答

1

確保您的腳本中有一個GameObject分配給LocalPlayer變量。該對象正在您的層次結構中查找不帶引號的名爲「cameraLookTarget」的內容。資本化問題。

我建議在Awake()方法中尋找一個LocalPlayer對象,如果它爲null,則使用Debug.Log(「沒有本地播放器分配」)來提醒自己實際上沒有分配它。