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
C# 如何从另一个类访问在协同程序中定义的变量?_C#_Unity3d_Coroutine_Path Finding_Game Development - Fatal编程技术网

C# 如何从另一个类访问在协同程序中定义的变量?

C# 如何从另一个类访问在协同程序中定义的变量?,c#,unity3d,coroutine,path-finding,game-development,C#,Unity3d,Coroutine,Path Finding,Game Development,问题是,我在网上找到了一个教程,正在进行寻路工作,航路点被添加到协同路由中,我需要访问在协同路由中定义的变量currentwaypoint public IEnumerator FollowPath() { Vector3 currentWayPoint = path[0]; while (true) { if (transform.position == currentWayPoint) {

问题是,我在网上找到了一个教程,正在进行寻路工作,航路点被添加到协同路由中,我需要访问在协同路由中定义的变量currentwaypoint

 public IEnumerator FollowPath()
{
      Vector3 currentWayPoint = path[0];
   

        while (true)
        {
            if (transform.position == currentWayPoint)
            {
                targetIndex++;


                if (targetIndex >= path.Length)
                {
                    targetIndex = 0;


                    path = new Vector3[0];

                    reached = true;
                 
                    yield break;
                }
                reached = false;
                currentWayPoint = path[targetIndex];
                lastDir = (currentWayPoint - transform.position).normalized;

            }                
            transform.position = Vector3.MoveTowards(transform.position, currentWayPoint, speed * Time.deltaTime);
            transform.rotation = Quaternion.Euler(0, 0, GetAngle(currentWayPoint));
            fov.SetAngle(GetAngle(currentWayPoint));


        yield return null;
        }
但是当我添加lastDir变量时,它只返回0,0,0,我猜这是它的默认值

所以我需要的是访问这个变量值,因为它在循环中更新

提前谢谢

你不能

改为在类级别定义它:

// The value you actually store and update
private Vector3 currentWayPoint;

// A public Read-Only property for everyone else
public Vector3 CurrentWayPoint => currentWayPoint;

public IEnumerator FollowPath()
{
    currentWayPoint = path[0];
   
    while (true)
    {
        if (transform.position == currentWayPoint)
        {
            targetIndex++;

            if (targetIndex >= path.Length)
            {
                targetIndex = 0;

                path = new Vector3[0];

                reached = true;
                 
                yield break;
            }

            reached = false;
            currentWayPoint = path[targetIndex];
            lastDir = (currentWayPoint - transform.position).normalized;
        }    
        
        transform.position = Vector3.MoveTowards(transform.position, currentWayPoint, speed * Time.deltaTime);
        transform.rotation = Quaternion.Euler(0, 0, GetAngle(currentWayPoint));
        fov.SetAngle(GetAngle(currentWayPoint));

        yield return null;
    }
}
因此,另一个脚本将被删除

var waypoint = someObject.GetComponent<YourClass>().CurrentWayPoint;

你能发送完整的脚本吗?谢谢,看来我把问题搞糟了:D