I have made a class to organize some data, and a class made up of several of those classes (Subclass?) so I can do something like...
classA.classB.value = 1;
However I can't figure out how to reference my classes. If anybody knows how I should go about it that would be great!
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;
}
}
You have to use the new
keyword to initialize both genome
and quantity
. You use the new
keyword for initialization when the class you are trying to initialize does not derive from MonoBehaviour
. Your Genome
and Quantity
classes does not derive from MonoBehaviour
so the new keyword is the proper way to do initialize them.
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;
}
Now if your Genome
and Quantity
classes both derive from MonoBehaviour
. You should never should the new keyword to initialize them.
For example:
class Gene :MonoBehaviour{
}
You don't use the new keyword for that, you use addComponent
or Instantiate
GameObject the script is attached to. And you should never have a constructor in the class if it will derive from MonoBehaviour
.
void Start()
{
genome = gameObject.AddComponent<Gene>();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.