简体   繁体   English

从另一个脚本获取Vector 3(C#Unity)

[英]Getting Vector 3 from another Script (C# Unity)

I have a script to position the player where fell off from the platform. 我有一个脚本来定位玩家从平台上掉下来的位置。

Platform Tracker.cs 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);
    }
}

As I debugged, platformTransform shows the value I need. 在调试时,platformTransform显示了我需要的值。 But its not reflecting in the code below. 但它没有反映在下面的代码中。

GameController.cs 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;
    }

Your help is much appreciated. 非常感谢您的帮助。 Please let me know if anything is not clear. 如果有任何不清楚的地方,请告诉我。

To access thePlatformTracker correctly, you need to use GetComponent. 要正确访问平台跟踪器,您需要使用GetComponent。 In GameController.cs Start() simply change: 在GameController.cs中,Start()只需更改:

thePlatformTracker = FindObjectOfType<PlatformTracker>();

To: 至:

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

Be sure and add the game controller object name between the GameObject.Find("") quotation marks. 确保在GameObject.Find(“”)引号之间添加游戏控制器对象名称。

Then you should be able to access the platformTransform no problem since thePlatformTracker is now correctly referencing the script. 然后你应该能够访问platformTransform没问题,因为thePlatformTracker现在正确地引用了脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM