如何用循环单位c#实例化后将唯一变量设置为预制?

问题描述 投票:-1回答:1

我想为每个用foreach循环实例化的预制巫婆设置变量给我的子预制类(ArchiveRow.cs)。我可以在其父级的转换中实例化子级,并访问其类(ArchiveRow.cs),但其父级中的所有子级都具有相同的变量,我不想要它!]

    foreach (var item in channels)
    {
        GameObject thisParent = Instantiate(parent, transform) as GameObject;

        foreach (var item2 in collections)
        {
            Instantiate(child);
            child.transform.parent = thisParent.transform;

            ArchiveRow archiveRowComponent = child.GetComponentInChildren<ArchiveRow>();
            archiveRowComponent.archiveId = item2.collectionId;

        }
    }

我的上面代码的结果看起来像=>

    thisParent(1) ->
        child(1) ==> archiveId = 100
        child(2) ==> archiveId = 100
        ...

    thisParent(2)->
        child(1) ==> archiveId = 200
        child(2) ==> archiveId = 200
        ...

        ...

但是我想要设置变量,例如=>

    thisParent(1) ->
        child(1) ==> archiveId = 100
        child(2) ==> archiveId = 150
        ...

    thisParent(2)->
        child(1) ==> archiveId = 200
        child(2) ==> archiveId = 250
        ...

        ...
c# unity3d instantiation
1个回答
0
投票

该问题可能是参考。

所以,

foreach (var item2 in collections)
{
    Instantiate(child);
    child.transform.parent = thisParent.transform;

    ArchiveRow archiveRowComponent = child.GetComponentInChildren<ArchiveRow>();
    var myid = item2.collectionId;
    archiveRowComponent.archiveId = myid;

}

应该修复它。

© www.soinside.com 2019 - 2024. All rights reserved.