Cannot implicitly convert type `UnityEngine.GameObject[]' to `UnityEngine.GameObject'
Hello, Can someone please tell me what I am doing wrong in this statement?
public GameObject[] arrayAll={GameObject.FindGameObjectsWithTag("car"), GameObject.FindGameObjectsWithTag("trafficlight"), GameObject.FindGameObjectsWithTag("zebracrossing")};
I think its somewhere else in the script. A place where you try to cram your array into a single GameObject variable.
Solve it by adding a [x] index of some kind to the gameObject variable in question, as stated by the line number in your error.
Replace FindGameObjectsWithTag
with FindGameObjectWithTag
Answer by srudgr8 · Dec 04, 2015 at 04:07 PM
@meat5000 that was the line i got an error. @frarees I tried replacing with FindGameObjectwithTag. This is my entire code.
using UnityEngine;
using System.Collections;
using System.Collections.Generic ;
using System.Linq ;
public class repl_two : MonoBehaviour {
public GameObject[] arrayAll;
public float percentage;
public float area;
public float height;
public float width;
public bool carnear=false;
public bool trafficnear=false;
public bool zebranear=false;
public bool carseen= false;
public bool trafficseen= false;
public bool zebraseen= false;
public float cardist;
public float trafficdist;
public float zebradist;
public float carcounter=0.0f;
public float trafficcounter=0.0f;
public float zebracounter=0.0f;
public bool moving=false;
public Vector3 previous;
public float vel;
public float speed;
void Update () {
arrayAll = {GameObject.FindGameObjectWithTag("car"),GameObject.FindGameObjectWithTag("car1"), GameObject.FindGameObjectWithTag("trafficlight"),GameObject.FindGameObjectWithTag("trafficlight1"), GameObject.FindGameObjectWithTag("zebracrossing"),GameObject.FindGameObjectWithTag("zebracrossing1")};
for(int i=0; i<arrayAll.Length; i++){
if ((arrayAll[i].CompareTag ("car")|| arrayAll[i].CompareTag("car1")) && arrayAll[i].renderer.isVisible) {
vel = ((arrayAll[i].transform.position - previous).magnitude) / Time.deltaTime;
previous = arrayAll[i].transform.position;
moving = true;
}
}
for(int i=0; i<arrayAll.Length; i++){
if (arrayAll[i].CompareTag ("car")||arrayAll[i].CompareTag("car1")) {
cardist = Vector3.Distance (arrayAll[i].transform.position, Camera.main.transform.position);
Camera.main.transform.position);
if (arrayAll[i].renderer.isVisible && cardist <= 5) {
carnear = true;
} else {
carnear = false;
}
}
}
for(int i=0;i<arrayAll.Length;i++){
if (arrayAll[i].CompareTag ("trafficlight")|| arrayAll[i].CompareTag("trafficlight1")) {
trafficdist = Vector3.Distance (arrayAll[i].transform.position, Camera.main.transform.position);
if (arrayAll[i].renderer.isVisible && trafficdist <= 5) {
trafficnear = true;
} else {
trafficnear = false;
}
}
}
for(int i=0; i<arrayAll.Length;i++){
if (arrayAll[i].CompareTag( "zebracrossing")|| arrayAll[i].CompareTag("zebracrossing1")) {
zebradist = Vector3.Distance (arrayAll[i].transform.position, Camera.main.transform.position);
if (arrayAll[i].renderer.isVisible && zebradist <= 5) {
zebranear = true;
//Debug.Log ("target is close");
} else {
zebranear = false;
}
}
}
for(int i=0; i<arrayAll.Length;i++){
if ((arrayAll[i].CompareTag("car")||arrayAll[i].CompareTag("car1")) && arrayAll[i].renderer.isVisible) {
carcounter += Time.deltaTime;
if (carcounter >= 3.0f && arrayAll[i].renderer.isVisible) {
carseen = true;
carcounter = 0.0f;
}
else {
carseen = false;
carcounter = 0.0f;
}
}
}
for(int i=0;i<arrayAll.Length;i++){
if (arrayAll[i].CompareTag ("trafficlight")|| arrayAll[i].CompareTag("trafficlight1")) {
if (arrayAll[i].renderer.isVisible) {
trafficcounter += Time.deltaTime;
if (trafficcounter >= 3.0f && arrayAll[i].renderer.isVisible) {
trafficseen = true;
trafficcounter = 0.0f;
}
else {
trafficseen = false;
trafficcounter = 0.0f;
}
}
}
}
for (int i=0; i<arrayAll.Length; i++) {
if ((arrayAll[i].CompareTag("zebracrossing")||arrayAll[i].CompareTag("zebracrossing1")) && arrayAll[i].renderer.isVisible) {
zebracounter += Time.deltaTime;
if (zebracounter >= 3.0f && arrayAll[i].renderer.isVisible) {
zebraseen = true;
zebracounter = 0.0f;
}
else {
zebraseen = false;
zebracounter = 0.0f;
}
}
}
}
void OnGUI(){
GUI.Box (new Rect (100, 0, 250, 250), "Perception");
for(int i=0; i<arrayAll.Length; i++){
if ((arrayAll[i].CompareTag("car")|| arrayAll[i].CompareTag("car1")) && vel >= 0.1 && arrayAll[i].gameObject.renderer.isVisible) {
GUI.Box (new Rect (100, 0, 250, 250), " \ncar moving at speed of " + vel);
}
}
for(int i=0; i<arrayAll.Length; i++){
if ((arrayAll[i].CompareTag ("car")||arrayAll[i].CompareTag("car1")) && carnear == true && cardist <= 5 && arrayAll[i].gameObject.renderer.isVisible ) {
GUI.Box(new Rect(100,0,250,250)," \n\ncar is close target"+cardist); }
}
for(int i=0;i<arrayAll.Length; i++){
if ((arrayAll[i].CompareTag("trafficlight")|| arrayAll[i].CompareTag("trafficlight1")) && trafficnear == true && trafficdist <= 5 && arrayAll[i].renderer.isVisible ) {
GUI.Box(new Rect(100,0,250,250)," \n\n\n trafficlight is close target"); }
}
for(int i=0; i<arrayAll.Length; i++){
if ((arrayAll[i].CompareTag("zebracrossing")|| arrayAll[i].CompareTag("zebracrossing1")) && zebranear == true && zebradist <= 5 && arrayAll[i].renderer.isVisible ) {
GUI.Box(new Rect(100,0,250,250)," \n\n\n\n zebracrossing is close target"); }
}
for(int i=0; i<arrayAll.Length; i++){
if ((arrayAll[i].CompareTag("car")||arrayAll[i].CompareTag("car1")) && carseen == true && arrayAll[i].renderer.isVisible) {
GUI.Box (new Rect (100, 0, 250, 250), "\n\n\n\n\n car viewed more than 3 sec");
}}
for(int i=0; i< arrayAll.Length; i++){
if ((arrayAll[i].CompareTag("trafficlight")|| arrayAll[i].CompareTag("trafficlight1")) && trafficseen == true && arrayAll[i].renderer.isVisible /*&&trafficcounter >2.5f*/) {
GUI.Box (new Rect (100, 0, 250, 250), "\n\n\n\n\n\n trafficlight viewed more than 3 sec");
}}
for(int i=0; i<arrayAll.Length;i++){
if ((arrayAll[i].CompareTag("zebracrossing")|| arrayAll[i].CompareTag("zebracrossing1")) && zebraseen == true && arrayAll[i].renderer.isVisible) {
GUI.Box (new Rect (100, 0, 250, 250), "\n\n\n\n\n\n\n zebra viewed more than 3 sec");
}}
}
I figured it out, thanks!
void Start(){ car = new GameObject[1]; car=GameObject.FindGameObjectsWithTag("car"); trafficlight = new GameObject[1]; trafficlight=GameObject.FindGameObjectsWithTag("trafficlight"); zebracrossing = new GameObject[1];
zebracrossing=GameObject.FindGameObjectsWithTag("zebracrossing");
}