2016-04-29 4 views
0

私はいくつかのデータを整理するクラスを作って、これらのクラスのいくつかの構成されたクラス(サブクラス?)ので、私はどのように把握することはできませんしかし、私のような...Unity3Dモノクラスのクラスからなるクラスをどのように参照しますか?

classA.classB.value = 1; 

何かを行うことができています私のクラスを参照してください。どのように私はそれについて行く必要があるか知っていれば、それは素晴らしいだろう!

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; 
    } 
} 

答えて

2

あなたはgenomequantityの両方を初期化するためにnewキーワードを使用する必要があります。初期化しようとしているクラスがMonoBehaviourから派生していない場合は、newキーワードを初期化に使用します。 GenomeQuantityのクラスは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; 


} 

は、今すぐあなたの GenomeQuantityクラスの両方が MonoBehaviourから派生している場合。新しいキーワードを初期化する必要はありません。例えば

class Gene :MonoBehaviour{ 

} 

あなたは、スクリプトが添付されてaddComponentまたはInstantiateゲームオブジェクトを使用し、そのための新しいキーワードを使用しないでください。そして、もしそれがMonoBehaviourから派生するならば、クラス内にコンストラクタを持つべきではありません。

void Start() 
{ 
    genome = gameObject.AddComponent<Gene>(); 
} 
+0

'Genome'と' Quantity'は 'AudioClip'の子孫ではありません。 OPの誤解を避けるためにいくつかの改訂を検討してください。 – Serlite

+0

実際にそれらを '新しいゲノム()/数量()'にするといいですよね) –

+0

@Serliteこれは私が私の側にスクリプトを持っていないのでコンパイルするのに使ったタイプミスです。ゲノムとゲノムに変更するのを忘れました – Programmer

関連する問題