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 Samirashul · Apr 14, 2018 at 04:10 PM · gameobjectarray2d array

Adding a Gameobject to an array makes all elements of that array equal to the object

I'm trying to make a 2d array, then start a loop where i create a tile and store that tile into my array, then start a new loop for debugging purposes which takes a tile based on its position in the array and checks in has the correct information (x and y position). however, upon debugging, it appears that all the tiles in that array now have the same x and y position as the very last one.

Here is my code:

 public GameObject emptySpace;
 GameObject[,] board;

 public void makeBoard (int x, int y){
     board = new GameObject[x, y];
     for (int i = 0; i < x; i++) {
         for (int j = 0; j < y; j++) {
             GameObject s = emptySpace as GameObject;
             OccupiableSpace o = s.GetComponent<OccupiableSpace> ();
             o.xPos = i;
             o.yPos = j;
             Instantiate (s, new Vector2 (i, j), Quaternion.identity);
             int id = i * y + j;
             print ("x is " + i + " , y is " + j + " ,space number " + id + " has been created");
             board [i, j] = s;
         }
     }
     for (int i = 0; i < x; i++) {
         for (int j = 0; j < y; j++) {
             GameObject s = board [i, j];
             OccupiableSpace o = s.GetComponent<OccupiableSpace> ();
             int id = o.xPos * y + o.yPos;
             //if (o.xPos == i && o.yPos == j) {
             print ("x is " + o.xPos + " , y is " + o.yPos + " ,space number " + id + " has been located");
             //}
         }
     }
 }

 void Awake (){
     makeBoard (3, 4);
 }

here is the console:

alt text

alt text

debug2.jpg (69.2 kB)
debug1.jpg (62.6 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

Answer by BastianUrbach · Apr 15, 2018 at 07:24 AM

In line 12 you clone s which creates a new GameObject but doesn't change the value of s. Instead, it returns the newly created object. Since you only ever use s afterwards, not the clone, you perform all operations on the same GameObject.

Replace line 12 with

s = Instantiate (s, new Vector2 (i, j), Quaternion.identity);
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 Samirashul · Apr 15, 2018 at 04:15 PM 0
Share

$$anonymous$$any thanks, i knew it was something dumb like that :)

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

122 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

Related Questions

2D Array of GameObjects... 1 Answer

How to check if a specific gameobject is in the array? 1 Answer

selected object in array lost in translation 0 Answers

How to remove objects from a list ? 3 Answers

How do you save GameObject[]? 1 Answer


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