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
-1
Question by uberokeer · Jun 30, 2013 at 05:50 AM · collisionarrayobjectsgridnaming

Keeping track of array objects

I'm making a game where i need to create a 68x68 grid of cubes that are invisible but are set as triggers. I already have the prefab and i know i need a array, But i can't seem to figure out how to keep track of every object in the array so i can access it later. Like maybe number each object differently. Thanks in advance.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Wiki

Answer by flamy · Jun 30, 2013 at 08:17 AM

There are 2 ways to do it.

one by naming each cube along with the x and y indices.

other by making use of the fact that it is a grid and calculate the distance between the first grid and this to calculate the index.

Let me explain the first method.

When you create the grid, name the objects like below

 cube_0_0
 cube_0_1
 .
 .
 .
 .
 cube_67_66
 cube_67_67

And in the code you can find the index by splitting the name of the object.

 void OnTriggerEnter(Collider other)
 {
 
    string[] splitString  = other.name.Split("_"[0]);
    //now at index zero the name cube will be there and in 1 and 2 it will be the index on x and y
 
   int indexX= System.Int32.Parse(splitString[1]);
   int indexY= System.Int32.Parse(splitString[2]);
 
   // do blah blah blah with indices...
 
 }

I usually dont prefer this method, because it takes lot of time to setup and also it is not a very feasible method, when lot of changes involve in code.

And the second method by using the distance...

Since the distance between any two adjacent objects in the same direction is always equal, you can find the distance between 2 the very first object of the grid and this and with this you can easily calculate the index of the object.

void OnTriggerEnter(Collider other) {

  Vector3 _distance = other.transform.position - firstObjectOfGrid.position; 

//firstObjectOfGrid could be linked to the editor or found using tag.

  int indexX = (int)(_distance.x/GridSizeOnX);   //GridSizeOnX is the constant size of grid on X, simply difference between any adjacent objects on X.
  int indexY = (int)(_distance.x/GridSizeOnY);

}

Note that in the manager script, you can do the same thing for adding to the array .

 public GameObject[,] allObjects
 
 void Awake()
 {
       //make sure to tag all objects as gridObject or similar..
       
       allObjects = new GameObject[68,68];
 
       GameObject[] tempArray = GameObject.FindObjectsWithTag("gridObject");
 
      for(int i=0;i<68;i++)           // it is not a good practice to use number straight away, declare a variable for this and do so that changes could be easily done.
      {
            for(int j=0;j<68;j++)
            {
                 Vector3 _distance = other.transform.position - firstObjectOfGrid.position; 
                 int indexX = (int)(_distance.x/GridSizeOnX);  
                 int indexY = (int)(_distance.x/GridSizeOnY);
            }
       }
 
 }

The second method is convenient in lot of ways but one draw back is, if the girds are not equally spaced.

the second method will be more convenient if the OnTriggerEnter is check on the Object and not player, because you can calculate its index in start and save and then when collision is happening you can just check if the collided object is the current object and do the calculation. The below is the sample...

 public class GridObject : Monobehaviour
 {
     public int indexX;
     public int indexY;
 
     void Start()
     {   
 
        Vector3 _distance = transform.position - Gameobject.Find("FirstGridObjet");.transform.position;   
 
        indexX = (int)(_distance.x/GridSizeOnX);  
        indexY = (int)(_distance.x/GridSizeOnY);
              
     } 
 
      void OnTriggerEnter(Collider other)
      {
            if(other.gameObject == gameObject)
            {
 
                  //send message to the manager or similar and do blah blah blah...
            }
      }
 
 }
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
avatar image
0

Answer by Nanity · Jun 30, 2013 at 07:07 AM

Sounds like a 2D array:

 // assign this one in the editor to your cube prefab:
 public Transform prefab;
 // Not sure about the brackets, afaik they belong there:
 private Transform[,] gridArray = new Transform[68,68]();
 
 public void initTriggerGrid()
 {
     for (int i = 0; i < 68; i++)
     {
         for (int j = 0; j < 68; j++)
         {
             gridArray[i,j] = Instantiate(prefab, new Vector3(i*1.0f, 0, j*1.0f), Quaternion.identity) as Transform;
         }
     }
 }
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

17 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

Related Questions

How to create a 2D grid based array with passable/impassable grids for movement ? 1 Answer

Help in solving mistake in my code 1 Answer

objects in Array null when changing scenes 1 Answer

Best way to manage a collision between a few different objects? 1 Answer

Collision detection of certain ojects 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