- Home /
Targeting Issue Error
Okay here is my Script (Using C#)
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Targetting : MonoBehaviour {
public List targets;
// Use this for initialization void Start () { targets = new List();
AddAllEnemies();
}
public void AddAllEnemies()
{
GameObject go =GameObject.FindGameObjectWithTag("Enemy");
foreach (GameObject enemy in go) AddTarget(enemy.transform); }
public void AddTarget(Transform enemy) { targets.Add(enemy); }
//Update is called once per frame void Update () {
} }
My Error: foreach statement cannot operate on variables of type unityengine gameobject because it does not contain a definition for 'GetEnumerator' or is not accessible
I'm clueless with Scripting never done it before but I'm in the process of learning so any help would be greatly appreciated, thanks =D
This is also my first of probably many posts on here heehee
Answer by mhtraylor · Apr 07, 2013 at 04:56 AM
Check the documentation for GameObject. It appears you've probably confused GameObject.FindWithTag()
with GameObject.FindGameObjectsWithTag()
. The former returns a single GameObject that has a given tag, the latter an array of these GameObjects. So your code here
GameObject go = GameObject.FindGameObjectWithTag("Enemy")
should probably be something like
GameObject[] goArray = GameObject.FindGameObjectsWithTag("Enemy")
You can then iterate over this array like you wish with foreach...
; also check out the MSDN docs about Collections, which has links on how to use native arrays and .NET collections, as well explaining the "GetEnumerator" business. Hope that helps.
Your answer
Follow this Question
Related Questions
Getting index of the smallest number in a list 1 Answer
NullReferenceException problem 2 Answers
Particle circle HELP 1 Answer
Missing Reference FPS help 1 Answer
Null in GetValidMethodInfo 0 Answers