如何从ienumerator更改全局变量 [英] How can I change a global variable from an ienumerator

查看:94
本文介绍了如何从ienumerator更改全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用IEnumerator更改多个布尔值(冻结为假)





I am using an IEnumerator to change multiple booleans for example (Frozen to be false)


public void ChangeStatus(ref bool status)
 {
 
     if(status == true)
     {
         TimeDelay(status);
 
     }
 }
 
 public IEnumerator TimeDelay(bool status)
 {
     yield return new WaitForSeconds(ResistanceLevel);
 
     status = false;
 
 }





我无法在IEnumerator中使用REF传递参数,因此我的程序无效。



任何解决方案谢谢。



Unity2d,C#



我尝试了什么:



我尝试过thread.sleep但是停止整个游戏。



I can not pass an argument using REF in an IEnumerator so my program is not working.

Any Solutions Thank you.

Unity2d,C#

What I have tried:

I have tried thread.sleep but that stops the whole game.

推荐答案

status 不是全局变量(C#设计中没有这样的东西,尽管 static 变量允许你近似它们 - 它是一个局部参数变量,只能在 ChangeStatus 方法中访问,它是内存中的位置不固定,并且会在应用程序运行时发生变化,因为它会在调用方法之前立即放在堆栈上。



你不能做你想做的事情to:我认为你需要在这里重新考虑你的整个方法,但我可以告诉你该怎么做因为我不知道为什么你会认为这个犯规对于您要解决的任何问题,d都是一个很好的解决方案!
status is not a "global variable" (there are no such things in C# by design, although static variables do allow you to approximate them) - it is a local parameter variable, and is only accessible inside the ChangeStatus method, it's location in memory is not fixed and will vary while the app is running because it is placed on the stack immediately before the method is called.

You cannot do what you want to: I think you need to rethink your whole approach here, but I can;t tell you what to do as I have no idea why you would think this would be a good solution to whatever problem you are trying to solve!


这篇关于如何从ienumerator更改全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆