首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何引用由Unity3D单行为类组成的类?

如何引用由Unity3D单行为类组成的类?
EN

Stack Overflow用户
提问于 2016-04-29 21:09:39
回答 1查看 167关注 0票数 0

我创建了一个类来组织一些数据,一个类由其中的几个类组成(子类?)这样我就能做些像..。

代码语言:javascript
复制
classA.classB.value = 1;

然而,我不知道如何引用我的课程。如果有人知道我该怎么做,那就太好了!

代码语言:javascript
复制
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;
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-29 21:17:50

您必须使用new关键字来初始化genomequantity。当要初始化的类不从new派生时,可以使用MonoBehaviour关键字进行初始化。您的GenomeQuantity类不是从MonoBehaviour派生的,因此新关键字是初始化它们的正确方法。

代码语言:javascript
复制
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;


}

现在,如果您的GenomeQuantity类都是从MonoBehaviour派生的。您永远不应该使用新的关键字来初始化它们。

例如:

代码语言:javascript
复制
class Gene :MonoBehaviour{

}

如果不使用新关键字,则使用脚本附加的addComponentInstantiate GameObject。如果类将从MonoBehaviour派生,则类中永远不应该有构造函数。

代码语言:javascript
复制
void Start()
{
  genome = gameObject.AddComponent<Gene>();
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36947479

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档