2015-02-07 72 views
2

我有一個遊戲的統一代碼,假設遊戲對象標籤名稱爲「玩家」,假設該遊戲遵循該字符。但我得到一個代碼錯誤,我不知道如何解決它。需要對象引用才能訪問非靜態成員`UnityEngine.GameObject.tag'

錯誤

Assets/Script/CameraRunner.cs(10,32): error CS0120: An object reference 
is required to access non-static member `UnityEngine.GameObject.tag' 

腳本

using UnityEngine; 
using System.Collections; 

public class CameraRunner : MonoBehaviour { 



void FixedUpdate() 
{ 
    if (GameObject.tag == "Player") { 
        transform.position = new Vector3 (0, transform.position.y + 9f, -10); 
      } 
} 
} 
+0

爲什麼不直接使用內置的相機SmoothFollow.js腳本或iTween?兩者都符合要求,你只需要稍微修改它。 iTween無處不在。 – ApolloSoftware 2015-02-07 07:45:20

+0

您是否還將腳本附加到主攝像頭,您想要的對象?如果將該腳本附加到相機,那麼GameObject是本地的,它只會檢測本地對象是否標記爲播放器。如果它在玩家身上,那麼對象的位置正在改變。腳本有很多錯誤,特別是沒有足夠的信息來制定適當的響應。 – ApolloSoftware 2015-02-07 07:51:32

回答

0

你爲什麼不只是使用內置的攝像頭SmoothFollow.js腳本或的ITween?兩者都符合要求,你只需要稍微修改它。 iTween無處不在。

你還將腳本附加到主攝像頭,你想要的對象?如果將該腳本附加到相機,那麼GameObject是本地的,它只會檢測本地對象是否標記爲播放器。如果它在玩家身上,那麼對象的位置正在改變。腳本有很多錯誤,特別是沒有足夠的信息來制定適當的響應。

在解決它的腳本中,執行以下操作並將其附加到您的相機。另外不要忘記設置你的播放器變量,並設置你的標籤,如果你不這樣做。

using UnityEngine; 
using System.Collections; 

public class CameraRunner : MonoBehaviour { 
public GameObject player; 

    void Start() { 
    if (player == null) 
     player = GameObject.FindWithTag("Player"); 
    } 

    void FixedUpdate() { 
    if (player != null) 
    transform.position = new Vector3 (0, player.transform.position.y + 9f, -10); 
    } 

} 
2

您需要使用gameObject.tag而不是GameObject.tag

相關問題