簡體   English   中英

如何從另一個 class 更改字符串值?

[英]How I can Change String Value from another class?

public class Class1
{
    public string word;
    public string GetString()
    {
       ....
    }

}




public class Class2 : MonoBehaviour
{

    public Class1[] class1;



    void Start()
    {
        class1 = new Class1[3];
        getText();
    }




    void getText()
    {
        for (int i = 0; i < words.Length; i++)
        {
            class1[i].word = "new Text";
            class1[i].GetString();

        }
    }
}

我如何從Class2更改(單詞)?

我收到此錯誤: NullReferenceException: Object reference not set to an instance of an object

當您初始化class1數組時,它的所有元素都是null ,您需要在嘗試訪問它們的成員之前單獨初始化它們。

void getText()
{
    for (int i = 0; i < words.Length; i++)
    {
        class1[i] = new Class1(); // Here!
        class1[i].word = "new Text";
        class1[i].GetString();

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM