我如何从 Unity3D 单一行为中引用由 class 组成的 class?
How do I reference a class made up of classes from a Unity3D monobehaviour?
我做了一个 class 来组织一些数据,还有一个 class 由几个 classes (Subclass?) 组成,所以我可以做一些类似...
classA.classB.value = 1;
但是我不知道如何引用我的 classes。如果有人知道我应该怎么做,那就太好了!
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
class Gene {
public string name;
public float value;
public float complexity;
public Gene() {
name = "";
value = 0;
complexity = 0;
}
public Gene(string Name, float Value, float Complexity) {
name = Name;
value = Value;
complexity = Complexity;
}
}
class Genome {
public Gene agility;
public Gene intelligence;
public Gene strength;
public Genome(){
agility = new Gene();
intelligence = new Gene();
strength = new Gene();
}
public Genome(Gene Agility, Gene Intelligence, Gene Strength) {
agility = Agility;
intelligence = Intelligence;
strength = Strength;
}
public IEnumerator GetEnumerator() {
return (IEnumerator)this;
}
}
public class Life : MonoBehaviour {
Genome genome; //Warning Life.genome is never assigned to, and will always have its default value null
Quantity quantity; //Warning Life.quantity is never assigned to, and will always have its default value null
void OnEnable() {
genome = /*???*/; //How do I add the reference?
quantity = /*???*/; //How do I add the reference?
genome.agility.name = "Agility"; //On Runtime: NullReferenceException: Object reference not set to an instance of an object
genome.agility.complexity = 100;
genome.intelligence.name = "Intelligence";
genome.intelligence.complexity = 1000;
genome.strength.name = "Strength";
genome.strength.complexity = 100;
}
}
您必须使用 new
关键字来初始化 genome
和 quantity
。当您尝试初始化的 class 不是从 MonoBehaviour
派生时,您可以使用 new
关键字进行初始化。您的 Genome
和 Quantity
classes 不是从 MonoBehaviour
派生的,因此 new 关键字是初始化它们的正确方法。
Genome genome = null;
Quantity quantity = null;
void Start()
{
//initialize
genome = new Genome(new Gene("", 0, 0), new Gene("", 0, 0), new Gene("", 0, 0));
quantity = new Quantity ();
//Now you can use both references
genome.agility.name = "Agility";
genome.agility.complexity = 100;
genome.intelligence.name = "Intelligence";
genome.intelligence.complexity = 1000;
genome.strength.name = "Strength";
genome.strength.complexity = 100;
}
现在,如果您的 Genome
和 Quantity
class 都派生自 MonoBehaviour
。你永远不应该使用 new 关键字来初始化它们。
例如:
class Gene :MonoBehaviour{
}
你不为此使用 new 关键字,你使用脚本附加到的 addComponent
或 Instantiate
GameObject。如果 class 派生自 MonoBehaviour
,那么你不应该在 class 中有构造函数。
void Start()
{
genome = gameObject.AddComponent<Gene>();
}
我做了一个 class 来组织一些数据,还有一个 class 由几个 classes (Subclass?) 组成,所以我可以做一些类似...
classA.classB.value = 1;
但是我不知道如何引用我的 classes。如果有人知道我应该怎么做,那就太好了!
using UnityEngine;
using System.Collections.Generic;
using System.Collections;
class Gene {
public string name;
public float value;
public float complexity;
public Gene() {
name = "";
value = 0;
complexity = 0;
}
public Gene(string Name, float Value, float Complexity) {
name = Name;
value = Value;
complexity = Complexity;
}
}
class Genome {
public Gene agility;
public Gene intelligence;
public Gene strength;
public Genome(){
agility = new Gene();
intelligence = new Gene();
strength = new Gene();
}
public Genome(Gene Agility, Gene Intelligence, Gene Strength) {
agility = Agility;
intelligence = Intelligence;
strength = Strength;
}
public IEnumerator GetEnumerator() {
return (IEnumerator)this;
}
}
public class Life : MonoBehaviour {
Genome genome; //Warning Life.genome is never assigned to, and will always have its default value null
Quantity quantity; //Warning Life.quantity is never assigned to, and will always have its default value null
void OnEnable() {
genome = /*???*/; //How do I add the reference?
quantity = /*???*/; //How do I add the reference?
genome.agility.name = "Agility"; //On Runtime: NullReferenceException: Object reference not set to an instance of an object
genome.agility.complexity = 100;
genome.intelligence.name = "Intelligence";
genome.intelligence.complexity = 1000;
genome.strength.name = "Strength";
genome.strength.complexity = 100;
}
}
您必须使用 new
关键字来初始化 genome
和 quantity
。当您尝试初始化的 class 不是从 MonoBehaviour
派生时,您可以使用 new
关键字进行初始化。您的 Genome
和 Quantity
classes 不是从 MonoBehaviour
派生的,因此 new 关键字是初始化它们的正确方法。
Genome genome = null;
Quantity quantity = null;
void Start()
{
//initialize
genome = new Genome(new Gene("", 0, 0), new Gene("", 0, 0), new Gene("", 0, 0));
quantity = new Quantity ();
//Now you can use both references
genome.agility.name = "Agility";
genome.agility.complexity = 100;
genome.intelligence.name = "Intelligence";
genome.intelligence.complexity = 1000;
genome.strength.name = "Strength";
genome.strength.complexity = 100;
}
现在,如果您的 Genome
和 Quantity
class 都派生自 MonoBehaviour
。你永远不应该使用 new 关键字来初始化它们。
例如:
class Gene :MonoBehaviour{
}
你不为此使用 new 关键字,你使用脚本附加到的 addComponent
或 Instantiate
GameObject。如果 class 派生自 MonoBehaviour
,那么你不应该在 class 中有构造函数。
void Start()
{
genome = gameObject.AddComponent<Gene>();
}