- Home /
Question by
Karisahaya · May 22, 2013 at 06:09 AM ·
gameobjectfindgameobjectswithtagwith
Cannot implicitly convert type `UnityEngine.GameObject[]' to `UnityEngine.GameObject'
How could i fix this problem? Here is the code:
using UnityEngine;
using System.Collections;
public class EnemyAI : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
private Transform myTransform;
void Awake() {
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectsWithTag("Player");
target = go.transform;
}
// Update is called once per frame
void Update () {
}
}
Comment
Best Answer
Answer by Fornoreason1000 · May 22, 2013 at 06:23 AM
um your assigning an array to a singular game objects
GameObject go = GameObject.FindGameObjectsWithTag("Player");
this line should be
GameObject go = GameObject.FindGameObjectWithTag("Player");
or
GameObject go = GameObject.FindGameObjectsWithTag("Player")[0];
Also for the Mods out there ,for some weird reason FindGameObjectWithTag
isn't documented, only its array is. Why is this?
http://docs.unity3d.com/Documentation/ScriptReference/GameObject.html
The singular version is GameObject.FindWithTag().
You could also do:
GameObject[] objects = GameObject.FindGameObjectsWithTag("Player");
I'm Guessing that means GameObject go = GameObject.FindGameObjectWithTag("Player");
is obselete? or they didn't bother with it because of GameObject.FindWithTag().
?