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 /
  • Help Room /
avatar image
0
Question by DevMagi · Feb 01, 2017 at 07:29 PM · gameobjectsarray of gameobjectsstatic-variables

trying to get gameobjects assigned to Array[x,y]

i have been looking at all the answers and i am really new at the unity so please bear with me.

if have 3 scripts. one to maintain an 2d array

 public class Grid
 {
     public static Element[,] elementArray = new Element[5, 5];
 }

this references a script i attached to a prefab gameobject:

 public class Element : MonoBehaviour {
     
 //starts at -1 to due to the loops in gamesetup script
 public static int locX = -1;
 public static int locY = -1;
     
 int x = (int)locX;
 int y = (int)locY;
 Grid.elementArray[locY, locX] = this;
 }

lastly i have a script to build up a variable sized puzzlefield with gameobjects:

 public void GenerateGrid()
     {
 
         GameObject cellInputField;
         RectTransform rowParent;
         
         for (int rowIndex = 0; rowIndex < numOfRows; rowIndex++)
         {
             rowParent = (RectTransform)Instantiate(panelRow);
             rowParent.transform.SetParent(Gridset);
             rowParent.transform.localScale = Vector3.one;
             Element.locY += 1;
             Element.locX = 0;
             for (int colIndex = 0; colIndex < numOfColumns; colIndex++)
             {
                 cellInputField = (GameObject)Instantiate(cellgrid);
                 cellInputField.transform.SetParent(rowParent);
                 cellInputField.GetComponent<RectTransform>().localScale = Vector3.one;
 
             }
             
         }

in the test i am trying to build a grid of 5 by 5 so 25 gameobjects but i 25 times the same error:

 IndexOutOfRangeException: Array index is out of range.
 Element.Start () (at Assets/Element.cs:37)

if i understand the message correctly i am still not passing the correct [x,y] location to the array so that the gameobject is stored in the correct position. but i am complete missing a solution to this issue. i hope anyone can help me.

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 DevMagi · Feb 01, 2017 at 10:31 PM

ah never mind i found the solution...

i changed the LocX and LocY away from Static and added the below the generateGrid part.

 Element instance = cellgrid.GetComponent<Element>();

this solved it. this way i can call the correct instances of LocX and LocY while creating the grid and update them correctly for each copy gameobject.

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

90 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

Related Questions

Get gameObjects from a protected GameObject array of base class in derived class 1 Answer

Showing different objects OnMouseDown 1 Answer

How can i add a gameobject configured already on a unity application without rebuild this? 0 Answers

destroying instantiated groups of objects 1 Answer

Bad practice to create a GameObject for every BoxCollider2D? 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