Am I losing scope within IF statements?

时间:2015-09-01 21:46:51

标签: c# unity3d

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);

            }
       }

1 个答案:

答案 0 :(得分:0)

我没有Unity3d,所以我不确定那里是否有什么东西,但我只是想知道是否有任何后台线程导致它超出了你破坏的线程的范围?即在你的IF语句之前,你在线程1中断并看到值。在IF语句中,您没有中断,因为当条件为真时,正在进行另一个线程?你可以通过使变量静态来测试这个。如果你看到这个值,你肯定会知道它是一个线程问题。

由于存在线程问题,您需要更多地容忍多个线程访问其属性而不会导致错误导致错误决策。有关这方面的更多信息,请访问:http://www.albahari.com/threading/