Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by GameSlayerGS · Mar 26, 2015 at 07:57 AM · gameobjectobjecttablecs0029

Assets/SkyAir.cs(13,25): error CS0029: Cannot implicitly convert type `UnityEngine.GameObject[]' to `UnityEngine.Object'

I get the error above from the script below, I sort of understand the problem, basically it won't let me assign a GameObject to a variable that's an array/table. What I don't understand is why I'm getting the error, somewhere in there it should get converted shouldn't it? I literally copied and pasted the code from this page: http://docs.unity3d.com/ScriptReference/GameObject.FindGameObjectsWithTag.html And then I changed the names on the variables and the tag so it actually fits in with my stuff. Help please. Thank you. :)

Alternatively if there's another way to compose a table/list/array (I don't really know what it's properly called in C# in Unity), I could do that. my end goal is to have the list called voxels to contain every GameObject with the tag "TerrainVoxel".

 using UnityEngine;
 using System.Collections;
 
 public class SkyAir : MonoBehaviour {
 
     public GameObject Air;
     public GameObject VoxelTerrain;
     public Object voxels;
 
     // Use this for initialization
     void Start () {
         if (voxels == null)
             voxels = GameObject.FindGameObjectsWithTag("TerrainVoxel");
 
         foreach (Object voxel in voxels) {
             if (voxel.transform.position.y >= 513) {
                 Instantiate (Air, voxel.transform.position, voxel.transform.rotation);
                 Destroy (voxel);
             }
         }
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }



Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by NoseKills · Mar 26, 2015 at 08:06 AM

You have to stay true to your variable types. FindGameObjectsWithTag() returns an array of GameObjects. You can't put an array into a UnityEngine.Object variable.

Even if you could, on the next line, how could the compiler know that this 'object' you create is something you can loop with a foreach?

Even if it did, inside the foreach loop, a UnityEngine.Object does not have a variable 'transform', so voxel.transform won't work either.

You have to declare 'voxels' a GameObject[] and 'voxel' a GameObject

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 GameSlayerGS · Mar 26, 2015 at 11:34 AM 0
Share

Thanks, doing what you suggested did solve the issue. However, I'm still confused as to why Unity's documentation would have had an example that didn't work. Though, if you don't know, don't worry about it.

avatar image
0

Answer by Darress · Mar 26, 2015 at 03:11 PM

Try this:

 public Object[] voxels;
Comment
Add comment · 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
0

Answer by MakakWasTaken · Mar 26, 2015 at 03:11 PM

Try with this instead: public GameObject Air; public GameObject VoxelTerrain; public GameObject[] voxels;

      // Use this for initialization
      void Start () {
          if (voxels == null)
              voxels = GameObject.FindGameObjectsWithTag("TerrainVoxel");
  
          foreach (GameObject voxel in voxels) {
              if (voxel.transform.position.y >= 513) {
                  GameObjet.Instantiate (Air, voxel.transform.position, voxel.transform.rotation);
                  Destroy (voxel);
              }
          }
      }
Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

inspector,Component 0 Answers

Destroy object when transform.position.x > 10. 1 Answer

Unity3D Pressure Plate request. 3 Answers

Instantiating gameObject with custom Class properties 1 Answer

using Contains(gameObject) to find and destroy a gameObject from a list 2 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