[英]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.