• 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 Decompiled · Dec 05, 2015 at 06:05 PM · gameobjectinstantiatearray

Reference GameObjects from an Array

I am creating a small tile game and I thought it would be fun to instantiate the tiles and store a reference to them in an array on start, but I am having a little trouble. I have tried looking this up, but I am really struggling to search for reinvent information.

In the below code I create an array, instantiate a tile, rename it so it matches the grid address I want to use and then I have tried to put it into the array. But the print functions all return 3-3, so it appears that I have added a reference to my Tile GameObject in my script rather than each array address referencing a different tile. I do intend to pass this array to another function once fixed, but how do I fix this?

 public class InstantiateGrid : MonoBehaviour {
 
     public GameObject Tile;
 
     void Start () {
     
     GameObject[,] tileArray = new GameObject[3,3];
     
     for (int i = 1; i < 4; i++) {
         for (int j = 1; j < 4; j++) {
             Object.Instantiate(Tile);
             Tile.name = i + "-" + j;
             tileArray[i,j] = Tile;
         }
     }

     print (tileArray[1,1]);
     print (tileArray[1,2]);
     print (tileArray[1,3]);
     }
 }
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 OncaLupe · Dec 06, 2015 at 01:26 AM

'Tile' is a reference to the prefab object, not the newly instantiated copy. All you're doing is renaming the prefab and adding a reference of the prefab to the array. You need a temp variable to store the instantiated reference to. Also, you're not assigning anything to the first slots of the array. All list/array style variables in coding start at 0. The provided code also gives an IndexOutOfRangeException error when trying to assign to slot 3 as the array goes from 0 to 2.

 using UnityEngine;
 using System.Collections;
 
 public class InstantiateGrid : MonoBehaviour
 {
     GameObject[,] tileArray = new GameObject[3,3];
     public GameObject TilePrefab;
     
     void Start () {
         GameObject tileInstance;
 
         for (int i = 1; i < 4; i++) {
             for (int j = 1; j < 4; j++) {
                 tileInstance = Instantiate(TilePrefab) as GameObject;
                 tileInstance.name = i + "-" + j;
                 tileArray[i-1, j-1] = tileInstance;
             }
         }
 
         print (tileArray[0,0]);
         print (tileArray[0,1]);
         print (tileArray[0,2]);
     }
 }
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 Decompiled · Dec 06, 2015 at 05:28 PM 0
Share

Thank you! That is really obvious now.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can i Instantiate gameobject in array 0 Answers

Spawning limited GameObjects at a specific position not working 1 Answer

Multiple Instantiations not working 0 Answers

Switching the index of a GameObject Array type? 2 Answers

,Instantiate at touch position 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