Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 floydtherooster · Sep 14, 2014 at 05:25 PM · instantiateprefabpositiongrid2d array

Need Help With 2D Array Grid Position Tracking With JavaScript

I'm creating a grid based puzzle game by using a 2D array that creates copies of some prefab objects. That works perfectly fine and I am able to get the game objects created and put into the level. However, I need help making it so that each instance of the prefabs know where they are in the array. I need them to be able to tell if they are at [0,0], [2,1, [1,0], etc so that they can modify their neighbors based on what is there. I've tried giving variables to each object and then assigning them once they are created but that didn't seem to work at all. And obviously I can't just rely on my nested for loop variables because those will be maxed at the end and useless. I'm a bit rusty with coding at the moment as well but I'm hoping there's an easy solution to get each copy to remember where they are in the array. Here is a copy of my array and level set up.

 //This will create a 2D array that will hold some spots and data for various squares
 //10 represents a blank square where a player can place an orb
 //0 represent a square filled with a blank orb
 var startX : int = -1; // this is the left hand side of the puzzle grid
 var startY : int = 1; // this is the top of the puzzle grid
 var gridArray = [
 [10, 0, 10],
 [10, 0, 0],
 [0, 0, 10] ];
  
 ///This will end up placing the correct objects in the level based on gridArray
 var r : int;
 var c : int;
 var gridW : int = gridArray.length; //gets the width of the gridArray for the for loops
 var gridH : int = gridArray[0].length; //gets the height of the gridArray for the for loops
 for(r = 0; r < gridW; r++)
 {
     for(c = 0; c < gridH; c++)
     {
         var currentX = startX + (r*.96); // this moves newsprites right for each new column being created
         var currentY = startY - (c*.96); // this moves new sprites down for each new row being created
          // 1 pixel seems to be .01 unity units. 100 pixels would be 1 unit
          
         // r and c are reversed in the if statements so the squares are placed left to right
         if(gridArray[c][r] == 10)
         {
                 Instantiate(obj_BlankSpot, Vector3 (currentX, currentY, 0), Quaternion.identity);  //this object is a blank square where a player can place an orb
                 blankSpotScript.xCoord = c; //blankSpotScript allows me to access the x and y variables in the script attached to obj_BlankSpot
                 blankSpotScript.yCoord = r; //blankSpotScript allows me to access the x and y variables in the script attached to obj_BlankSpot
                 //Need this to save it's unique position in gridArray[c][r]
         }
    
         else if(gridArray[c][r] == 0)
         {
                 Instantiate(obj_PlacedBlankOrb, Vector3 (currentX, currentY, 0), Quaternion.identity); // this object is a square that hold a blank orb that can be broken
                 blankOrbScript.xCoord = c; //blankOrbScript allows me to access the x and y variables in the script attached to obj_PlacedBlankOrb
                 blankOrbScript.yCoord = r; //blankOrbScript allows me to access the x and y variables in the script attached to obj_PlacedBlankOrb
                 //Need this to save it's unique position in gridArray[c][r]
         }
     }
 }


//////// Update: ////////

So I tried making some adjustments to the code and assigning the individual squares different variables that were set to r and c at their time of creation but the values are getting all mixed up and it's not working out how I intended. Basically the blank squares and blank orbs now have an xCoord and yCoord value that I was trying to set to r and c. I'm thinking that maybe when it's assigning the values to xCoord and yCoord that it's choosing the wrong prefab instances?

I put the new if/instantiate code up above. Any ideas? I guess I could try and figure out each instance ID and then specify that instance ID in the variable assignment somehow, but I'm not sure how I would go about doing that.

////////Second Update://///////
Since I was having issues with getting the values for each prefab instance when trying to assign the values in the for loops, I tried having the prefab instances themselves get the values from the for loop variables r and c themselves. However, even though r and c are obviously incrementing in order to go through the loops, the variables in the prefab instances always stay at 0. Here is the code for one of the prefabs:

 var selectedOrbScript : scr_gridArray;
 var xCoord : int = selectedOrbScript.c; //sets xCoord = to the c value in the loops for the 2D array
 var yCoord : int = selectedOrbScript.r; //sets yCoord = to the r value in the loops for the 2D array
  
 function OnMouseDown ()
 {
     print (xCoord);
     print (xCoord);
 }

When I mouse click on them, the values are 0 and 0 for all 9 grid spaces and i can't figure out why. I've searched for the answer to this on here and I can't find anything specifically saying how to just keep each prefab position separate and identifiable in the array. This is the only thing keeping me from moving forward and finishing the project at this point so if anyone has an idea, please let me know.

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

Answer by floydtherooster · Sep 16, 2014 at 07:27 AM

I was finally able to fix the problem. Using the method in the second update where I was having each instance of the prefab try and set it's variables to r and c but ended up getting 0, I found out that the code within the prefab instances weren't executing until the array script was entirely finished. So scr_gridArray was completely finishing before the variables scr_BlankSpot and scr_BlankOrb (the scripts that are attached to obj_Blankspot and obj_PlacedBlankOrb) were even starting. A friend told me to try the Awake function, and using that, the variables were able to be set properly while the array grid was still being created. The new code from the scr_BlankOrb (my second code segment from above) now looks like this:

 var gridArrayScript : scr_gridArray;
 var xCoord : int;
 var yCoord : int;
  
 function Awake()
 {
         xCoord = gridArrayScript.r; //sets xCoord = to the c value in the loops for the 2D array
         yCoord = gridArrayScript.c; //sets yCoord = to the r value in the loops for the 2D array
 }
  
 function OnMouseDown ()
 {
     print (xCoord);
     print (yCoord);
 }
 create a new version of this paste
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

2 People are following this question.

avatar image avatar image

Related Questions

How to instantiate the prefabs on grid in Unity? 1 Answer

Why can't I instantiate an object that is +5 in the x and z axis? 1 Answer

GameObject change Position after game started 1 Answer

Collider.bounds position is wrong? 3 Answers

How can customize which prefabs are instantiated in a grid? 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