[英]Cannot access a nonstatic member of outer type
public class ScoreManager : MonoBehaviour
{
public Text recordd;
string filePath;
string jsonString;
[System.Serializable]
public class Personaje
{
public string nombre;
public string profesion;
public int nivel;
public override string ToString ()
{
return string.Format ("{0}: {1} nivel {2}", nombre, profesion, nivel);
}
}
[System.Serializable]
public class ListaPersonajes
{
public List<Personaje> personajes;
public void Listar () {
//loop cada objeto lista
foreach (Personaje personaje in personajes) {
Debug.Log(personaje);
Debug.Log(personaje.nombre);
recordd.text = "" + personaje; //// THIS LINE ERROR WHY??
}
}
}
}
错误:
错误CS0038:无法通过嵌套类型“ ScoreManager.ListaPersonajes”访问外部类型“ ScoreManager”的非静态成员
您正在尝试访问父类中记录的字段。 该字段未标记为静态(您可能不希望它是静态的),因此访问是非法的。 您需要有一个ScoreManager实例才能访问redordd
编辑:
public class Program
{
public static void Main()
{
ScoreManager scoreManager = InitializeMonoBehaviour(); // This is what you need to figure out, how to properly init the object.
List<Personaje> personajes = new List<Personaje>();
/*
fill personajes
*/
foreach (Personaje personaje in personajes)
{
Debug.Log(personaje);
Debug.Log(personaje.nombre);
scoreManager.AppendText(personaje.ToString());
}
}
}
public class ScoreManager : MonoBehaviour
{
public Text recordd;
string filePath;
string jsonString;
publlic void AppendText(string text)
{
this.recordd.text += text;
}
}
[System.Serializable]
public class Personaje
{
public string nombre;
public string profesion;
public int nivel;
public override string ToString ()
{
return string.Format ("{0}: {1} nivel {2}", nombre, profesion, nivel);
}
}
我删除了ListaPersonajes类(目前没有找到原因),并向ScoreManager类添加了AppendText(string text)方法。 初始化类后,可以遍历列表并附加文本。 我还用对ToString()的调用替换了“” + personaje语法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.