- Home /
Scripting issue...
im using UNITY 3.5.1 and scripting in c# in monodevelop i
m working on a targeting script, but am constantly getting an error report when i try to run the game.. the error report is: Assets/dkscripts/Targeting.cs(17,30): error CS0029: Cannot implicitly convert type UnityEngine.GameObject' to
UnityEngine.GameObject[]'
the script is:
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Targeting : MonoBehaviour { public List targets;
// Use this for initialization
void Start () {
targets = new List<Transform>();
AddAllEnenmies();
}
public void AddAllEnenmies()
{
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 () {
}
}
Any help would be good..
Answer by syclamoth · May 14, 2012 at 02:41 PM
The problem is this line:
GameObject[] go = GameObject.FindGameObjectWithTag("Enemy");
GameObject.FindGameObjectWithTag only returns one GameObject! You need to use
GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
I'm honestly a bit surprised it compiles at all- the scripting reference doesn't mention anything about a 'FindGameObjectWithTag'- it's just 'FindWithTag' for a single object.
Answer by kolban · May 14, 2012 at 02:42 PM
Your subject line is vague. It doesn't contain any hint as to what kind of scripting error we will find in here. You show that an error was found on line 17 but you don't tell us which line is line 17.
I am assuming that your pasted code is exactly your code as found in Unity.
I see the following line:
GameObject[] go = GameObject.FindGameObjectWithTag("Enemy");
What I notice is the singular in the method call. A plural might be:
GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
It is possible that your code is returning only the first object with the given tag and not all objects with the given tag.
THanks guys for your help worked fine after that silly mistake of $$anonymous$$e.. hehehehehe..
Your answer
Follow this Question
Related Questions
unity 3.4 crashes when i get to a certain point in the character generator script? 0 Answers
Unity Real Time Carving, does it work ? 1 Answer
Help me to fix my script error cs1519 1 Answer
How to delete animation event from .fbx file using script 0 Answers
Swapping animation clips at runtime 1 Answer