我想为每个用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
...
...
该问题可能是参考。
所以,
foreach (var item2 in collections)
{
Instantiate(child);
child.transform.parent = thisParent.transform;
ArchiveRow archiveRowComponent = child.GetComponentInChildren<ArchiveRow>();
var myid = item2.collectionId;
archiveRowComponent.archiveId = myid;
}
应该修复它。