Task i am trying to accomplish.
(1) Find all the GameObjects in the scene with the tag “Enemy”, and turn that into a list.(gos list)
(2) if the other list (trs list) count is less than the gos list, instance a new gameobject and add its transform to the trs list
can anyone see why I am getting an “Argument out of range” exception at line 42 here after one value is added to the trs list ?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
[System.Serializable]
public class GOb
{
public List<GameObject> gos = new List<GameObject>();
public List<Transform> trs = new List<Transform>();
}
public class listingTest : MonoBehaviour
{
public List<GOb> GOBS = new List<GOb>();
// Update is called once per frame
void Update()
{
CheckAndSet();
}
void CheckAndSet()
{
foreach (var item in GOBS.Select((x, i) => new { Value = x, Index = i }))
{
item.Value.gos = (GameObject.FindGameObjectsWithTag("Enemy")).ToList();
if (item.Value.trs.Count < item.Value.gos.Count)
{
GameObject enemyRep = new GameObject("Enemy");
item.Value.trs.Add(enemyRep.transform);
Debug.Log("added");
}
for (int i = 0; i < item.Value.gos.Count; i++)
{
Debug.Log(item.Value.trs*);*
}
}
}
}