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…
FindObjectsWithTag is missing an s.
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.
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.