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 /
avatar image
0
Question by Lariosss · Mar 19, 2020 at 10:27 AM · 2dinstantiatearrayarray of gameobjects2d array

Issue with instantiating multiple clone GameObjects

Hi, I'm trying to create a 50x50 2d Array of tiles, it works perfectly however in the Hierarchy after I run the code, I get a bunch of "New Game Object" GameObjects with no properties.

Could you tell me why and eventually how to fix it? Everything else in the code works fine and gets me the desired results.

Thank you.

alt text

 public class Matrice : MonoBehaviour
 {
     public GameObject referenceTile;
     
     
     GameObject[,] griglia = new GameObject[50, 50];
     Vector2 grigliaPos;
     // Start is called before the first frame update
     void Start()
     {
         
         generaGriglia();
         
     }
 
    
 
     void generaGriglia()
     {
         for (int i = 0; i < 50; i++)
         {
             for (int j = 0; j < 50; j++)
             {
                 GameObject gridSpace = new GameObject();
                 gridSpace = referenceTile;
                 float posX = i -24.5f;
                 float posY = j -24.5f;
                 gridSpace.name = "X: " + posX + " " + "Y: " + posY;
                 gridSpace.gameObject.tag = "MapTiles";
                 griglia[i, j] = (GameObject)Instantiate(gridSpace, new Vector3(posX, posY, 0), Quaternion.identity);
                 
                 
 
 
             }
 
         }
         
     }
 
      
 }
 


issue.png (42.1 kB)
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
1
Best Answer

Answer by Casiell · Mar 19, 2020 at 10:49 AM

 GameObject gridSpace = new GameObject();

So here you are creating new, empty gameobject (not sure why). This is the New Game Object in your hierarchy

 gridSpace = referenceTile

Here you override the reference to that object with your prefab reference

 gridSpace.name = "X: " + posX + " " + "Y: " + posY;
 gridSpace.gameObject.tag = "MapTiles";

Here you modify that prefabs (not prefab instance, prefab itself) properties. You really shouldn't do that like ever.

 griglia[i, j] = (GameObject)Instantiate(gridSpace, new Vector3(posX, posY, 0), Quaternion.identity);

Here you finally instantiate the prefab.

So here is how this part of the code should look like:

 GameObject gridSpace = (GameObject)Instantiate(referenceTile, new Vector3(posX, posY, 0), Quaternion.identity); //instantiate prefab. creating new GameObject is absolutely unnecessary
 float posX = i -24.5f;
 float posY = j -24.5f;
 gridSpace.name = "X: " + posX + " " + "Y: " + posY; //modify INSTANCE properties, not the prefab itself
 gridSpace.gameObject.tag = "MapTiles";
 griglia[i, j] = gridSpace; //add the object to your array
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 Lariosss · Mar 21, 2020 at 07:19 PM 0
Share

Had to put

 GameObject gridSpace = (GameObject)Instantiate(referenceTile, new Vector3(posX, posY, 0), Quaternion.identity); //instantiate prefab. creating new GameObject is absolutely unnecessary

below

  float posX = i -24.5f;
  float posY = j -24.5f

due to undeclared variable error, however everything else worked perfectly.

Thank you very much, you solved my problem.

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

283 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Array of GameObjects... 1 Answer

Problem with object generation script? 1 Answer

2D Array's 1 Answer

Multiple Fire Points 2 Answers

How to remove null's from 2d array ? It's removing but only 2 out of 4 null's. 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