为什么我的completeText
变量在if
语句中失去了作用域?如果completeText
在语句之外使用,它就会起作用,这使我相信我需要在将其用作语句中的标签之前声明一些东西。
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,所以我不确定那里是否有什么事情发生,但我只是想知道是否有任何背景线程导致它超出了你正在打破的线程的范围?即在你的IF语句之前,你在线程1中中断并查看值。在IF语句中,你不会中断,因为当条件为真时,另一个线程正在进行中?您可以通过将变量设置为静态来测试这一点。如果您看到该值,您就知道这肯定是线程问题。
对于线程问题,你的类需要更容忍多个线程访问它的属性,而不会因为不同步而导致错误的决定。更多相关信息请访问:http://www.albahari.com/threading/