在我能做的所有事情上,对编程攻略来说还是很新的,但最终还是遇到了麻烦。正确的是,我正在尝试将功能附加到我的英雄上,这样我可以更轻松地进行序列化。但是,每当我尝试添加时,它都会显示
NullReferenceException:对象引用未设置为对象的实例 LevelUp.Update()(位于Assets / LevelUp.cs:62) 并且不增加测试能力。所以这是脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Hero
{
public string heroname;
public List<Ability> heroability = new List<Ability>;
public Hero(string name)
{
heroname = name;
{
能力等级
[System.Serializable]
public class Ability
{
public string abilityname;
public float abilitydmg;
public Ability(string name, float damage)
{
abilityname = name;
abilitydmg = damage;
}
}
,然后在无效更新上添加到外部(游戏对象已设置为持有人)
public class LevelUp : MonoBehaviour {
public GameObject heroOne;
public Hero hero1;
public Ability a1;
public List<Ability> test = new List<Ability>();
void Update()
{
hero1 = heroOne.GetComponent<FirstHolder>().hero1;
hero1.heroability.Add(new Ability("test", 1));
}
,这是错误的出处。任何见识将不胜感激。谢谢!