C# 如何在Update()unity c中更改条件#

C# 如何在Update()unity c中更改条件#,c#,visual-studio,unity3d,navmesh,C#,Visual Studio,Unity3d,Navmesh,我已经创建了navMeshand代理。对于target我使用了两个空对象。 我为每个空对象创建了两个按钮 如果我再次单击白色按钮代理移动到空目标,我将单击红色按钮代理移动到第二个空目标 当我想将代理从target-2移动到target-1时,我遇到了一个问题。 我如何将该代理转移到taeget-1? 视频链接 代码 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 使用UnityEngine.AI; 公共类SampleAgentScrip

我已经创建了navMeshand代理。对于target我使用了两个空对象。 我为每个空对象创建了两个按钮

如果我再次单击白色按钮代理移动到空目标,我将单击红色按钮代理移动到第二个空目标

当我想将代理从target-2移动到target-1时,我遇到了一个问题。 我如何将该代理转移到taeget-1?

视频链接

代码

使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.AI;
公共类SampleAgentScript:MonoBehavior{
公共转型目标,target2;
NavMesh代理;
私有静态bool start1=false,start2=false;
void Start()
{
agent=GetComponent();
}
公共静态无效按钮单击()
{
//如果单击白色按钮
start1=真;
}
公共静态无效按钮CLICK2()
{
//如果红色按钮点击
start2=真;
}
无效更新()
{
if(start1)//如果白色按钮点击移动到targer-1
{
agent.SetDestination(目标位置);
}
if(start2)//如果红色按钮点击移动到targer-2
{
agent.SetDestination(target2.position);
}
}
}
这可能会有所帮助

public static void buttonClick()
{
      //if white button click
    start1 = true;
    start2 = false;
}

public static void buttonClick2()
{
     //if red button click
    start2 = true;
    start1 = false;
}

单击第二个按钮时,两个条件都变为真,并且在每个帧中设置两个不同的目的地

public Transform dest, target , target2;

public void buttonClick()
{
     dest = target;
}

public void buttonClick2()
{
     dest = target2;
}

void Update()
{
     agent.SetDestination(dest .position);
}

您忘记了通过将布尔值重置为false来替换状态。由于在按钮单击处理程序中设置了布尔值,因此也可以在更新函数中交替状态

void Update()
{
    if (start1) //if white button click moves to targer-1
    {
        agent.SetDestination(target.position);
        start1=false;
    }

    if (start2) //if re button click moves to targer-2
    {
        agent.SetDestination(target2.position);
        start2=false;
    }
}

如何在静态方法中使用非静态成员?如果创建实例,那么它将不工作。AFAIK这是不可能的,静态方法无法访问非静态字段。顺便说一句,您的按钮单击函数不需要是
静态的
按钮单击函数,我正在从另一个脚本文件调用它。对于这个调用,我使用了静态函数,你可以从任何脚本中调用它,它不需要是
静态的
,要了解如何做,你可以查看unity教程您好,先生,请帮我解决这个问题,先生,请帮我解决这个问题,
void Update()
{
    if (start1) //if white button click moves to targer-1
    {
        agent.SetDestination(target.position);
        start1=false;
    }

    if (start2) //if re button click moves to targer-2
    {
        agent.SetDestination(target2.position);
        start2=false;
    }
}