如何在使用循环统一 C# 实例化后将唯一变量设置为预制件?

How set unique variable to prefab after instantiate with loop unity c#?

我想为每个预制 实例化 foreach 循环设置变量到我的 child 预制 class ( ArchiveRow.cs )。 我可以在他们的 parent 的转换中实例化 child 并访问他们的 class ( ArchiveRow.cs ) 但他们 parent 中的所有 child 都相同变量 我不想要它!

    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
        ...

        ...

问题很可能是引用。

所以,

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

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

}

应该修复它。