Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从另一个脚本获取向量3(C#Unity)_C#_Unity3d - Fatal编程技术网

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

从另一个脚本获取向量3(C#Unity),c#,unity3d,C#,Unity3d,我有一个脚本来定位从平台上掉下来的玩家 平台跟踪器.cs public GameObject platformTrackerPoint; public Vector3 platformTransform; public PlayerMovement thePlayerMovement; void Start() { platformTrackerPoint = GameObject.Find("PlatformTrackerPoint"); thePlayerMovemen

我有一个脚本来定位从平台上掉下来的玩家

平台跟踪器.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);
    }
}
公共游戏对象平台TrackerPoint;
公共矢量平台转换;
公众游戏者的运动,观众的运动;
void Start()
{
platformTrackerPoint=GameObject.Find(“platformTrackerPoint”);
thePlayerMovement=FindObjectOfType();
}
无效更新()
{
if((transform.position.x
在我调试时,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;
    }
公共平台跟踪器平台跟踪器;
公共矢量位置;
无效开始(){
platformStartPoint=platformGenerator.position;
playerStartPoint=thePlayer.transform.position;
平台跟踪器=FindObjectOfType();
thePlayer=FindObjectOfType();
}
公共无效重新授权玩家()
{
thePlayer.transform.position=平台跟踪器.platformTransform;
}

非常感谢你的帮助。如果有任何不清楚的地方,请告诉我。

要正确访问平台跟踪器,您需要使用GetComponent。在GameController.cs Start()中,只需更改:

thePlatformTracker = FindObjectOfType<PlatformTracker>();
platformtracker=FindObjectOfType();
致:

thePlatformTracker=GameObject.Find(“”.GetComponent();
确保并在GameObject之间添加game controller对象名称。查找(“”)引号


然后您应该能够访问platformTransform,没有问题,因为PlatformTracker现在正正确引用脚本。

要正确访问PlatformTracker,您需要使用GetComponent。在GameController.cs Start()中,只需更改:

thePlatformTracker = FindObjectOfType<PlatformTracker>();
platformtracker=FindObjectOfType();
致:

thePlatformTracker=GameObject.Find(“”.GetComponent();
确保并在GameObject之间添加game controller对象名称。查找(“”)引号


然后您应该能够访问platformTransform,这没有问题,因为PlatformTracker现在正确地引用了脚本。

的可能重复。您需要使用
GameController
获取组件,然后从中访问
tempPosition
变量<代码>GameObject.Find(“GameControllerObJ”).GetComponent().thePlatformTracker
。请在下次发布前搜索。是否可以在平台跟踪器组件中附加到GameController组件中的平台跟踪器字段?在可能的重复中。您需要使用
GameController
获取组件,然后从中访问
tempPosition
变量<代码>GameObject.Find(“GameControllerObJ”).GetComponent().thePlatformTracker。请在下次发布前搜索。是否可以在平台跟踪器组件中附加到GameController组件中的平台跟踪器字段?在检查员窗口