unity3d 示例化一个预制件,然后为每个预制件设置不同的变量

huus2vyu  于 2023-04-21  发布在  其他
关注(0)|答案(1)|浏览(141)

我试图在示例化时为我的每个预制件设置一个特殊的“int”变量ID。
我目前正在使用2个脚本。我的预制处理程序脚本和游戏处理程序脚本。我在游戏处理程序脚本中设置变量并使用random.range生成随机数,我还有一个函数来确保我的预制件的ID不相同。这很有效。现在我正在尝试分配我在游戏处理程序脚本中的值,即prefabIdOne,prefabIdTwo,prefabIdThree添加到预制处理程序脚本中的prefabId变量。
我已经尝试使用Prefab.GetComponent<prefabHandler>,但是我假设这将针对所有的预制件,所以不会工作。我也尝试针对gameObject而不是脚本,但是因为我假设这也不起作用。
我的代码的完整运行在这里:
设置变量:

horses = 0;
        horseOneRaceId = Random.Range(1 , 7);
        horseTwoRaceId = Random.Range(1 , 7);
        horseThreeRaceId = Random.Range(1 , 7);
        horseFourRaceId = Random.Range(1 , 7);
        horseFiveRaceId = Random.Range(1 , 7);
        horseSixRaceId = Random.Range(1 , 7);
        while (horseTwoRaceId == horseOneRaceId)
        {
            horseTwoRaceId = Random.Range(1 , 7);
        }
        
        while(horseThreeRaceId == horseOneRaceId || horseThreeRaceId == horseTwoRaceId)
        {
            horseThreeRaceId = Random.Range(1 , 7);
        }

        while (horseFourRaceId == horseOneRaceId || horseFourRaceId == horseTwoRaceId || horseFourRaceId == horseThreeRaceId)
        {
            horseFourRaceId = Random.Range(1 , 7);
        }

        while (horseFiveRaceId == horseOneRaceId || horseFiveRaceId == horseTwoRaceId || horseFiveRaceId == horseThreeRaceId || horseFiveRaceId == horseFourRaceId)
        {
            horseFiveRaceId = Random.Range(1 , 7);
        }

        while (horseSixRaceId == horseOneRaceId || horseSixRaceId == horseTwoRaceId || horseSixRaceId == horseThreeRaceId || horseSixRaceId == horseFourRaceId || horseSixRaceId == horseFiveRaceId)
        {
            horseSixRaceId = Random.Range(1 , 7);
        }

HorseHandler脚本具有此变量

public static int raceId;

这就是我在示例化时尝试示例化和赋值变量的方式:

if (horses == 0)
    {
        horseOne = Instantiate(_HorsePrefab);
        _HorsePrefab.name = "Horse (1)";
        horseOne.GetComponent<HorseHandler>().RealRaceID = horseOneRaceId;
        
        horses++;
    }
    if (horses == 1)
    {
        horseTwo = Instantiate(_HorsePrefab);
        _HorsePrefab.name = "Horse (2)";
        horseTwo.GetComponent<HorseHandler>();
        HorseHandler.raceId = horseTwoRaceId;
        horses++;
    }
    if (horses == 2)
    {
        horseThree = Instantiate(_HorsePrefab);
        _HorsePrefab.name = "Horse (3)";
        horseThree.GetComponent<HorseHandler>();
        HorseHandler.raceId = horseThreeRaceId;
        horses++;
    }
    if (horses == 3)
    {
        horseFour = Instantiate(_HorsePrefab);
        _HorsePrefab.name = "Horse (4)";
        horseFour.GetComponent<HorseHandler>();
        HorseHandler.raceId = horseFourRaceId;
        horses++;
    }
    if (horses == 4)
    {
        horseFive = Instantiate(_HorsePrefab);
        _HorsePrefab.name = "Horse (5)";
        horseFive.GetComponent<HorseHandler>();
        HorseHandler.raceId = horseFiveRaceId;
        horses++;
    }
    if (horses == 5)
    {
        horseSix = Instantiate(_HorsePrefab);
        _HorsePrefab.name = "Horse (6)";
        horseSix.GetComponent<HorseHandler>();
        HorseHandler.raceId = horseSixRaceId;
        horses++;
    }

这是我正在示例化对象的GameHandler脚本的检查器:

所有示例化的游戏对象都具有相同的raceId,它总是horseRaceIdSix的值(最后一个分配给HorseHandler.race的值)
任何帮助将非常感谢:)

ssm49v7z

ssm49v7z1#

所以我实际上在这里使用这个链接读了一点:https://answers.unity.com/questions/235836/what-exactly-is-static.html
可能对大多数人来说是基本的,但对于初学者甚至没有使用静态/非静态做过很多事情的人来说。

Static被分配给类,这意味着我示例化的预制件的所有版本共享相同的值。
非静态将允许您为预制件(或任何游戏对象)提供不同的值,即使具有相同的脚本。

当尝试像这样调用静态变量时:

horseFour.GetComponent<HorseHandler>();
    HorseHandler.raceId = horseFourRaceId;

您将收到一个错误,要求您给予一个对象引用,由于非静态字段。
使用下面的代码horseFour.GetComponent<HorseHandler>().raceId = horseFourRaceId;删除了错误,我假设这是因为你首先调用gameobject,然后在一个示例中获取组件,然后是脚本,然后是变量,而不是像上面那样将脚本作为一个独立的对象引用。

相关问题