我是否在IF语句中失去作用域? [英] Am I losing scope within IF statements?

查看:112
本文介绍了我是否在IF语句中失去作用域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的completeText变量在if语句中失去了作用域?如果completeText在语句外使用,它将起作用,这使我相信我需要先声明一些内容,然后才能将其用作语句内的标签.

Why is my completeText variable losing its scope within the if statements? If completeText is used outside the statements it works, which leads me to believe I need to declare something prior to using it as a label within the statement.

public class OnClick : MonoBehaviour
    {
        public bool isClicked = false;
        string cdrw = "CD-RW";
        public string txt;
        public string completeText = "";
        public GameObject cdrwModel;
        public StreamReader reader = null;
        public FileInfo theSourceFile = null;

    public void Start()
    {
        theSourceFile = new FileInfo(Application.dataPath + "/puzzles.txt");
        if (theSourceFile != null && theSourceFile.Exists)
            reader = theSourceFile.OpenText();
        if (reader == null)
        {
        Debug.Log("puzzles.txt not found or not readable");
        }
        else 
        {
           while ((txt = reader.ReadLine()) != null)
           {
                Debug.Log("-->" + txt);
                completeText += txt;
            }
         }
      }

      public void OnMouseEnter() {
          isClicked = true;
          completeText += txt;
      }

      public void OnGUI() {
      if ((isClicked) && (cdrwModel)){

            GUI.contentColor = Color.white;
            GUI.Label(new Rect(15, 35, 400, 400), "Press <TAB> for more information");
            if (Input.GetKey(KeyCode.Tab))
            {
                GUI.contentColor = Color.red;
                GUI.Label(new Rect(1000, 50, 400, 400), completeText);

            }
       }

推荐答案

我没有Unity3d,所以我不确定那里是否正在发生任何事情,但是我只是想知道是否有任何后台线程导致了它超出您要中断的线程的范围? IE.在IF语句之前,您进入线程1并看到该值.在IF语句中,您不会中断,因为当条件为true时,另一个线程正在进行中?您可以通过将变量设为静态进行测试.如果看到该值,您肯定会知道这是一个线程问题.

I don't have Unity3d, so I'm not sure if there's something going on there, but I was just wondering if there are any background threads causing it to be out of scope for the thread at which you're breaking? I.e. ahead of your IF statement, you break in thread 1 and see the value. In the IF statement, you don't on break because when the condition is true a different thread is in progress? You could test this by making the variable static. If you see the value, you'll know it's a threading issue for sure.

由于存在线程问题,您的类将需要更宽容地使用多个线程访问其属性,而又不会因不同步而导致错误的决策.有关此问题的更多信息,请访问: http://www.albahari.com/threading/

With a threading issue, you're class will need to be more tolerant to multiple threads accessing its properties without getting out of sync causing errant decisions. More information on this can be found at: http://www.albahari.com/threading/

这篇关于我是否在IF语句中失去作用域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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