C# 如何从另一个类访问在协同程序中定义的变量?
问题是,我在网上找到了一个教程,正在进行寻路工作,航路点被添加到协同路由中,我需要访问在协同路由中定义的变量currentwaypointC# 如何从另一个类访问在协同程序中定义的变量?,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) {
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