为什么我无法从课程中访问我的脚本变量?

时间:2015-09-21 11:03:08

标签: unity3d unityscript

正如话题所说。我无法从同一个脚本中创建的类中访问变量。 它在同一个脚本中,变量(如您所见)是公共的。有任何想法吗? 尝试谷歌搜索"如何从另一个类访问脚本变量"但是没有找到任何东西。

代码:

public var Ludnosc = new Array();
var humanCount : int;

public class Human {
    public var id : byte;
    public var creatureType = "HUMAN";
    public var gender : boolean; // false = k, true = m
    //public var firstname : String; <- Opcja do wprowadzenia później
    //public var lastname : String; <- Opcja do wprowadzenia później
    public var age : byte;
    public var pregnant : boolean = false;
    function Breed(partner) {
        if(this.age<16) {
        Debug.Log("Woman with id " + this.id + " is too young to be pregnant. She must be 16 or older.");
        }
        else {
            var success = Random.Range(0.0, makePregnantChance);
            Debug.Log("Breed chance of partners with IDs [" + this.id + ", " + partner + "] was " + success*100 + "%.");
            if(success>0.50) {
                this.pregnant = true;
                Debug.Log("Creature of type " + this.creatureType + ", with ID " + this.id + " is pregnant!");
                Ludnosc.push(new Human()); //LINE 44 | tworzymy nowego czlowieczka
                var tempHuman = Ludnosc[humanCount+1] as Human //LINE 45
                tempHuman.id = humanCount+1; //LINE 46
                tempHuman.age = 1;
                var losujPlec = Random.Range(0.0, 1.0);
                tempHuman.makePregnantChance = 18/tempHuman.age;
                }
            }
    }
    public var parents : byte[]; //Najpierw podajemy ID matki, potem ID ojca.
    public var makePregnantChance : float;
}

错误:

Assets/TextPierwszy.js(44,33): BCE0005: Unknown identifier: 'Ludnosc'.
Assets/TextPierwszy.js(45,49): BCE0005: Unknown identifier: 'Ludnosc'.
Assets/TextPierwszy.js(45,57): BCE0005: Unknown identifier: 'humanCount'.
Assets/TextPierwszy.js(46,48): BCE0005: Unknown identifier: 'humanCount'.

1 个答案:

答案 0 :(得分:2)

您的代码在这里:

public var Ludnosc = new Array();
var humanCount : int;

public class Human {...

实际需要:

public class Human {
    public var Ludnosc = new Array();
    var humanCount : int;

当然,如果他们与班级相关,如果没有,那么你最好还是创建另一个班级来举行其他任何事情HumanExtras(或类似的东西),例如。