【问题标题】:Unity Trouble with Lists列表的统一问题
【发布时间】:2015-09-20 15:02:21
【问题描述】:

所以我现在在 Unity 工作,我正在使用列表来保持我的价值观,但我遇到了一些麻烦。以下是相关功能:

List<DNA> generation;

// Use this for initialization
void Start () {
    generation = new List<DNA> ();
    generation.Add (new DNA ());
    generation [0].init ();
    Debug.Log (generation[0].Chromosone);
}

这个 Debug 发出 Null。

在我的 DNA 课上:

public void init(){

    this.Chromosone = new BitArray (36);

    for(int i = 0; i  < this.Chromosone.Length; i++){
        if(Random.Range(0,2) == 1)
            this.Chromosone.Set(i,true);
        else
            this.Chromosone.Set(i,false);
    }

    lifeTime = Random.Range (1, 3);
    Debug.Log (Chromosone);

}

这个 Debug 发出它是一个 BitArray。

编辑:如果我返回 Chromosone,我会得到 BitArray 作为返回。但是当我失去链接的那一刻,(下一行),Chromosone 就不复存在了。

编辑编辑:将列表更改为 DNA 数组可以解决问题,但我需要修改长度。所以这没有多大帮助......

【问题讨论】:

  • 我只是在没有看到可能的解释后自己对此进行了测试,并且两者都设置为非空值。尝试使 Chromosone 成为一个属性,并在设置器上放置一个断点以查看它被设置为什么。 (您发布的内容之外的内容会造成干扰)
  • Chromosone 已经是一个属性,当一个函数调用另一个函数时,外部的东西怎么会干扰?
  • 嗯,您在此处发布的其他内容导致了这种情况,因为我将副本作为控制台应用程序,它运行良好。 Add a breakpoint like this 并检查 value 的值。如果为null,则跟随调用堆栈查找原因。
  • 我弄错了,它不是属性。 Set 来自 BitArray 本身。
  • 如果它是一个字段,您可以轻松地将其设置为如图所示的属性。

标签: c# list unity3d bitarray


【解决方案1】:

我在 Unity 中使用 bool[] 而不是 BitArray 重新创建了这些脚本,因为这是更习惯的做法。您将使用的代码是:
主类:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class MainClass : MonoBehaviour {

    private List<DNA> generation = new List<DNA>();
    // Use this for initialization
    void Start () {
        generation.Add(new DNA());
        generation[0].init();
        for(int i = 0; i < generation[0].boolLength; i++)
        {
            Debug.Log(generation[0].chromosone[i]);
        }
    }

    // Update is called once per frame
    void Update () {

    }
}

DNA 类:

using UnityEngine;
using System.Collections;

public class DNA : MonoBehaviour {

    public bool[] chromosone;
    public int boolLength = 36;
    public int lifeTime;

    public void init()
    {
        this.chromosone = new bool[boolLength];
        for(int i = 0; i < boolLength; i++)
        {
            if(Random.Range (0,2) == 1)
                this.chromosone[i] = true;
            else
                this.chromosone[i] = false;
        }
        lifeTime = Random.Range(1,3);
    }
}

当然,我假设您只在问题中粘贴了相关代码,因此请务必添加回其余部分。但是需要注意的一点是,在 MainClass 中确保包含 using System.Collections.Generic 行,以便您可以访问 List 类型。
编辑:
如果您必须使用 BitArray,您只需将 DNA 类更改为:

using UnityEngine;
using System.Collections;

public class DNA : MonoBehaviour {

    public BitArray chromosone;
    public int boolLength = 36;
    public int lifeTime;

    public void init()
    {
        this.chromosone = new BitArray(boolLength);
        for(int i = 0; i < boolLength; i++)
        {
            if(Random.Range (0,2) == 1)
                this.chromosone.Set(i, true);
            else
                this.chromosone.Set(i, false);
        }
        lifeTime = Random.Range(1,3);
    }
}

我猜你的问题是,你要么没有正确地迭代染色体变量来调试它,因为它不为空,或者......你没有包含using System.Collections.Generic

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    • 2011-04-04
    • 2018-03-12
    • 1970-01-01
    相关资源
    最近更新 更多