【问题标题】:I can't get Dictionary from Start function to Update function我无法从 Start 函数获取 Dictionary 到 Update 函数
【发布时间】:2021-02-10 09:37:51
【问题描述】:

我想从 .txt 文件中获取一个可以使用字典索引 (lineDict["00000001"]) 调用的字符串。我在 Start 函数中创建了该部分,它可以从 txt 文件(StreamReader)调用字符串并将值赋给 Dictionary 罚款。 但是我不能从更新函数中调用那个字典。我尝试为第一部分创建一个新类,没有任何改变。 lineDict 调试好,但仍然不能从不同的地方调用。 (当前上下文中不存在名称“lineDict”)。

这是我一直坚持的代码。

using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.UI;

public class HandleTextFIle : MonoBehaviour
{
    public Text m_MyText;
    public Button YesButton;
    public Button NoButton;

    public void Start()
    {
        StreamReader inp_stm = new StreamReader("Assets/Resource/Roxxy.txt");
        string line;
        Dictionary<string, string> lineDict = new Dictionary<string, string>();
        while ((line = inp_stm.ReadLine()) != null)
        {
            string indexToAdd = line.Substring(0, 8);
            //Debug.Log("index to add: " + indexToAdd);

            string lineToAdd = line.Substring(9);
            //Debug.Log("line to add: " + lineToAdd);
            lineDict.Add(indexToAdd, lineToAdd);
        }
        inp_stm.Close();
        //Debug.Log("Dict return : " + lineDict["00000000"]);
    }

    public void Update()
    {
        m_MyText.text = lineDict["00000000"];

        if (Input.GetButton("YesButton") == true)
        {
            m_MyText.text = lineDict["00000001"];
        }
        else if (Input.GetButton("NoButton") == true)
        {
            m_MyText.text = lineDict["00000002"];
        }
        
    }
}

它调用的txt文件中的字符串只是

00000000 >AWAITING INPUT
00000001 >Do you hear me?
00000002 >INPUT RECEIVED

也许我错过了一些基本的东西。我不知道。请发送帮助或仅指出我错过的明显内容。提前致谢。

【问题讨论】:

    标签: unity3d


    【解决方案1】:

    与其他所有语言一样,变量也有一个范围,如果您在函数中定义变量,它将仅在该函数中可见,并且在该函数完成后,在内部创建的变量将被销毁。 因此,如果要在其他函数中使用字典,则必须将字典声明为全局变量。

    public class HandleTextFIle : MonoBehaviour
    {
         public Text m_MyText;
         public Button YesButton;
         public Button NoButton;
         private Dictionary<string, string> lineDict;
    
         public void Start()
         {
             StreamReader inp_stm = new StreamReader("Assets/Resource/Roxxy.txt");
             string line;
             lineDict = new Dictionary<string, string>();
             while ((line = inp_stm.ReadLine()) != null)
             {
                 string indexToAdd = line.Substring(0, 8);
                 //Debug.Log("index to add: " + indexToAdd);
    
                 string lineToAdd = line.Substring(9);
                 //Debug.Log("line to add: " + lineToAdd);
                 lineDict.Add(indexToAdd, lineToAdd);
             }
             inp_stm.Close();
             //Debug.Log("Dict return : " + lineDict["00000000"]);
         }
    

    编辑:这是大多数语言中的一个基本概念,如果您对这些东西仍然有点不放心,我建议您阅读一些解释基本原理的教程。 这是一篇关于variable scopes的文章。

    【讨论】:

      猜你喜欢
      • 2021-08-18
      • 2021-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-27
      • 1970-01-01
      • 2017-08-02
      相关资源
      最近更新 更多