2016-11-09 45 views
1

我有一個腳本位置從平臺掉下來的球員。獲取從另一個腳本(C#團結)矢量3

平臺Tracker.cs

public GameObject platformTrackerPoint; 
public Vector3 platformTransform; 

public PlayerMovement thePlayerMovement; 

void Start() 
{ 
    platformTrackerPoint = GameObject.Find("PlatformTrackerPoint"); 

    thePlayerMovement = FindObjectOfType<PlayerMovement>(); 
} 

void Update() 
{ 
    if ((transform.position.x < platformTrackerPoint.transform.position.x) && thePlayerMovement.grounded) 
    { 
     platformTransform = gameObject.transform.position; 
     Debug.Log ("Plaform Transform =" + platformTransform); 
    } 
} 

正如我調試,platformTransform顯示我需要的價值。但它不反映在下面的代碼中。

GameController.cs

public PlatformTracker thePlatformTracker; 

    public Vector3 tempPosition; 

void Start() { 
     platformStartPoint = platformGenerator.position; 
     playerStartPoint = thePlayer.transform.position; 

     thePlatformTracker = FindObjectOfType<PlatformTracker>(); 
     thePlayer = FindObjectOfType<PlayerMovement>(); 
    } 


    public void RespawnPlayer() 
    { 
     thePlayer.transform.position = thePlatformTracker.platformTransform; 
    } 

你的幫助是非常讚賞。如果有任何不清楚的地方,請告訴我。

+4

的可能的複製[如何從另一個腳本通過GetComponent另一遊戲對象訪問變量?](http://stackoverflow.com/questions/26551575/how-to-access-a-variable-from-another-script -in-另一個-遊戲物體貫通GETCO)。你需要使用'GameController'來獲取GetComponent,然後從它中訪問'tempPosition'變量。 'GameObject.Find( 「GameControllerObJ」)。GetComponent ()。thePlatformTracker'。請在下次發帖之前進行搜索。 – Programmer

+0

可能在平臺跟蹤工具組件被安裝到現場thePlatformTracker在GameController組件?在檢查員窗口中 –

回答

0

要正確訪問thePlatformTracker,你需要使用GetComponent。在GameController.cs開始()簡單的改變:

thePlatformTracker = FindObjectOfType<PlatformTracker>(); 

要:

thePlatformTracker = GameObject.Find("").GetComponent<Platform Tracker>(); 

確保並添加GameObject.Find( 「」)引號之間的遊戲控制器對象名稱。

那麼你應該能夠訪問platformTransform沒有問題,因爲thePlatformTracker現在正確引用的腳本。