I’m creating “two” arrays:
-
The first is simply an array of gameobjects with a specific tag
-
For the second (which is actually a generic list) I instantiate one prefab for every item that exists in the first array eg. if there are 7 items, 7 prefabs are instantiated. I then populate the generic list with all the instantiated prefabs.
Now what I would like to do is iterate through the generic list and parent each item to one item in the first array. But for some reason I can only manage to parent all the generic list items to one item in the array. Here is what I have so far:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class MapObjects {
public string objectTag;
public GameObject iconPrefab;
public GameObject[] objectsTagged;
public List<GameObject> iconPrefabs;
}
public class Map: MonoBehaviour {
public MiniMapObjects[] miniMapObjects;
// Use this for initialization
void Start () {
foreach(var element in miniMapObjects) {
element.objectsTagged = GameObject.FindGameObjectsWithTag(element.objectTag);
foreach(var taggedObjects in element.objectsTagged) {
GameObject objects = (GameObject) Instantiate(element.iconPrefab, transform.position, transform.rotation);
element.iconPrefabs.Add(objects);
}
for(var i = 0; i < element.iconPrefabs.Count; i++) {
icon.transform.parent = taggedObjects.transform;
}
}
}
The only similar solutions I can find online is parenting multiple array items to a single object, but I can’t find any solutions about parenting multiple items to other multiple items. So how could I do this?