[英]Getting Vector 3 from another Script (C# Unity)
我有一個腳本來定位玩家從平台上掉下來的位置。
Platform 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;
}
非常感謝您的幫助。 如果有任何不清楚的地方,請告訴我。
要正確訪問平台跟蹤器,您需要使用GetComponent。 在GameController.cs中,Start()只需更改:
thePlatformTracker = FindObjectOfType<PlatformTracker>();
至:
thePlatformTracker = GameObject.Find("").GetComponent<Platform Tracker>();
確保在GameObject.Find(“”)引號之間添加游戲控制器對象名稱。
然后你應該能夠訪問platformTransform沒問題,因為thePlatformTracker現在正確地引用了腳本。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.