public bool Tip=false; public GameObject gameTips; Start is called before the first frame update void Start Update is called once per frame void Update
要让以上代码只执行一遍,可以在Start()方法中添加一个标志位来判断是否已经执行过。
可以将Tip的初始值设置为true,并在Start()方法中将Tip设置为false。然后在Update()方法中,只有当Tip为true时才执行代码,并将Tip设置为false,以确保代码只执行一次。
下面是修改后的代码:
public bool Tip = true;
public GameObject gameTips;
// Start is called before the first frame update
void Start()
{
Tip = false;
}
// Update is called once per frame
void Update()
{
if (Tip)
{
gameTips.SetActive(true);
Tip = false;
}
else
{
gameTips.SetActive(false);
}
}
这样,代码只会在Start()方法中执行一次,并且只有在Tip为true时才会执行。执行后将Tip设置为false,确保代码只执行一次
原文地址: http://www.cveoy.top/t/topic/igzs 著作权归作者所有。请勿转载和采集!