Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Brokenarrow · Dec 08, 2015 at 10:56 PM · raycastarraygridcoordinatescoordinate-system

Creating a Grid using an Array with C#

Hi, I'm trying to create a grid with identical prefabs (which hoevwer need to have some sort of coordinate system). I've looked at examples and came up with the following (using raycast to detect the objects in the grid):

 using UnityEngine;
 using System.Collections;
 
 public class GridGenerator : MonoBehaviour {

     public GameObject gridSpace;
     GameObject[,] boardArray = new GameObject[10,10];
   
 
     void Start () {
 
         GenerateGrid ();
     }

     void Update () {
 
         Ray detectobject = Camera.main.ScreenPointToRay (Input.mousePosition);
         RaycastHit hit;
         if (Physics.Raycast (detectobject, out hit, 100)) 
         {
             Debug.DrawLine (detectobject.origin, hit.point);
 
         }

     }
 
     void GenerateGrid()
 
     {
 
         for (int i = 0; i < boardArray.GetLength(0); i++) 
         {
             for (int j = 0; j < boardArray.GetLength (1); j++) 
             {
 
                 boardArray[i,j] = Instantiate(gridSpace, new Vector3(j * 2,0,i * 2), Quaternion.identity);
 
             }
         }
     }
 }
 

I get the error: "error CS0266: Cannot implicitly convert type UnityEngine.Object' to UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)" I'm not sure what is meant by this.

Regards, Rob

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

1 Reply

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

Answer by NoseKills · Dec 09, 2015 at 04:47 PM

I wonder if you tried to search for an answer before asking, since I get a full page of exactly the same answer to this exact problem when i Google the error you mention. It's better than waiting 17 hours for a new answer ;-)

Instantiate() can be used to create many kinds of Objects, not just GameObjects like the Unity manual examples show.

In this non-generic version of the Instantiate() method, the compiler can't know which kind of objects you are instantiating, so you have to tell it to the compiler explicitly by casting whatever Instantiate produces into a GameObject.

 boardArray[i,j] = Instantiate(gridSpace, new Vector3(j * 2, 0, i * 2), Quaternion.identity) as GameObject

i.e. you are trying to put an Object into an array of GameObjects, which can't be allowed since not all Objects are GameObjects.

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 Brokenarrow · Dec 10, 2015 at 09:47 AM 0
Share

I did search, however I was not sure what to look for. I did not know I had to cast my array objects back to GameObjects. Thanks for the help.

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

39 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Creating a grid that holds objects of different sizes? 0 Answers

array of raycasts in grid based game 0 Answers

assigning several positions and call array of raycasts 0 Answers

I'm almost finished with my game, but I have a major bug that makes it basically unplayable, can someone help please 0 Answers

Keeping track of dynamic positions of items in a grid using List/Array? 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