我知道这个问题已经问了很多,甚至在查看例子时我仍然无法理解
我收到的错误是
“NullReferenceException:对象引用未设置为对象的实例”
我不明白为什么。
我想要做的是使用按钮创建一个类数组,这样当我按下按钮时,它会向数组添加另一个实例。但我觉得我似乎无法初始化我的阵列。这是我的代码:
public class Buttonmanager : MonoBehaviour
{
Basecharacter character;
Basequest Quest;
public Dropdown Recuiting;
public Dropdown questing;
public Text TxtCharname;
Basecharacter[] characters;
int x = 0;
public void start()
{
characters = new Basecharacter[20];
}
public void charactergen()
{
characters[x] = gameObject.AddComponent<Basecharacter>() as Basecharacter;
character = characters [x];
x++;
StartCoroutine (debugging ());
textrecuitdisplay ();
//TxtCharname = gameObject.GetComponent<UnityEngine.UI.Text> ();
TxtCharname.text = "Yo yo";
}
这是Basecharacter的代码。这就是我想要制作的数组:
public class Basecharacter : MonoBehaviour
{
public string Charactername;
public string ClassName;
public string Gender;
public string Race;
public string Guildrank;
//public Rank namerank;
public int Strength;
public int Dexterity;
public int Vigor;
public int Perception;
public int Intelligence;
public int Conviction;
public int Charisma;
public int loyalty;
public string trait1;
public string trait2;
public string trait3;
public bool awayonquest;
// Use this for initialization
void Start ()
{
int whilenum = 1;
getclass temp = gameObject.AddComponent<getclass>() as getclass;
ClassName = temp.getrandomclass ();
Getname tempname = gameObject.AddComponent<Getname> () as Getname;
Charactername = tempname.getrandomname ();
Getgender tempgen = gameObject.AddComponent<Getgender> () as Getgender;
Gender = tempgen.getrandomgender ();
Getrace temprace = gameObject.AddComponent<Getrace> () as Getrace;
Race = temprace.getrandomrace ();
Gettrait temptrait1 = gameObject.AddComponent<Gettrait> () as Gettrait;
trait1 = temptrait1.getrandomtrait ();
Gettrait temptrait2 = gameObject.AddComponent<Gettrait> () as Gettrait;
trait2 = temptrait2.getrandomtrait ();
while (whilenum == 1)
{
if (trait1 == trait2)
{
Gettrait whiletrait2 = gameObject.AddComponent<Gettrait> () as Gettrait;
trait2 = whiletrait2.getrandomtrait ();
Debug.Log ("I changed! 2");
}
else
whilenum = 2;
}
Gettrait temptrait3 = gameObject.AddComponent<Gettrait> () as Gettrait;
trait3 = temptrait3.getrandomtrait ();
while (whilenum == 1 || whilenum == 2)
{
if (trait3 == trait2 || trait3 == trait1)
{
Gettrait whiletrait3 = gameObject.AddComponent<Gettrait> () as Gettrait;
trait3 = whiletrait3.getrandomtrait ();
Debug.Log ("I changed! 3");
}
else
whilenum = 3;
}
Getrank temprank = gameObject.AddComponent<Getrank> () as Getrank;
Guildrank = temprank.getrandomrank ();
Strength = Random.Range (0, 10);
Dexterity = Random.Range (0, 10);
Vigor = Random.Range (0, 10);
Perception = Random.Range (0, 10);
Intelligence = Random.Range (0, 10);
Conviction = Random.Range (0, 10);
Charisma = Random.Range (0, 10);
loyalty = 50;
awayonquest = false;
}
// Update is called once per frame
void Update ()
{
}
}
根据错误的问题行是
characters [x] = gameObject.AddComponent()as Basecharacter;
我在这条线上做错了还是我之前遗漏了什么?我真的需要帮助。
我确实有另一个版本,其中字符是一个游戏对象,我正在尝试将基本字符的数据放在该游戏对象上,然后创建该数据,并在按下该按钮时添加数据。但是当我按下按钮时会创建游戏对象,但不会应用出现错误的数据
数组索引超出范围。
如果你认为这会更容易解决,我会发布代码。错误的原因是在一个类似的地方,似乎是相同的原因。但它只是提出了一个不同的错误。
最后的计划是能够在屏幕上的文本框中显示来自基本字符的数据的各个部分,并且当从下拉框中选择时,它会发生变化。但我目前仍然坚持这一部分,无法继续。 如果有人可以提供帮助,我将非常感激。