Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by srudgr8 · Dec 04, 2015 at 10:35 AM · array of gameobjects

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")};

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image meat5000 ♦ · Dec 04, 2015 at 10:39 AM 0
Share

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.

avatar image frarees · Dec 04, 2015 at 10:41 AM 0
Share

Replace FindGameObjectsWithTag with FindGameObjectWithTag

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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");
             }}

}

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image srudgr8 · Dec 06, 2015 at 03:36 PM 0
Share

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");
 }
 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Accessing same script through array of objects 1 Answer

How to access array inside another array? 0 Answers

Order of elements in array going back to default after being set in different order 1 Answer

Turning on the particles group at once 0 Answers

assigning gameobjects to an 2Darray 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges