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 Voenskjall · Mar 21, 2015 at 08:43 PM · gameobjectarrayspawnclasstile

Spawning GameObjects with help of classes --- Attaching classes to GameObjects

Hello! It's my very first time asking a question here, I haven't found out how to solve this problem, thank you very much!

I have this class ( in TileControl.cs )

 public class Tiles{
 
         public GameObject tileGO;
         public float xPos;
         public float yPos;
         public bool  isLight;
 
     }

I have a 2-dimensional array called TilePosition, which is empty, so the script Levels.cs fills it. Here's an example from Levels.cs

 if (levelNumber == 0)
         {
             rows = 3; columns = 4;
             TileControl.tilePosition = new int[,]  {{ 0, 1, 1, 1 },
                                                     { 1, 1, 1, 0 },
                                                     { 0, 1, 1, 1 }};
         }

And if I call BuildMap.GenerateMap( Levels.rows, Levels.columns ) it works as you can see here:

alt text

Here is the actual problem: If all the tiles are called ''Cube'', how do I distinguish them? How could I attach the class Tiles to every Cube?

If all the cubes had xPos, yPos and isLight variables, I could easily work with them. For example, I could delete all the cubes whose xPos is 2, or color them red, etc.

( Sorry for my English or if I wasn't clear ) Thank you!

( There are 9 tiles in the picture, but 18 in the Hierarchy, because there will be dark tiles behind light tiles, but that is not important )

tilelevel.png (109.9 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
Best Answer

Answer by Cherno · Mar 21, 2015 at 08:49 PM

How about this:

Add a bool "filled" to your Tiles class. Then instead of a 2-dimensional int array you create a 2-dimensional tiles array and for each Tiles in it, set the filled value to true or false (what was 0 or 1 in the int array). Now you can access each cube by passing the x and y values to the Tiles array.

Comment
Add comment · Show 3 · 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 Voenskjall · Mar 22, 2015 at 09:05 AM 0
Share

Thank you for your help Cherno, I'm going to try it and reply later

avatar image Voenskjall · Mar 22, 2015 at 11:10 AM 0
Share

Great, it works, thank you, but now I've encountered another problem. The tiles are deleted whenever I try to refer to them outside of the void Generate$$anonymous$$ap.

This is my Tiles array:

 TileControl.Tiles[,] tilePosition = new TileControl.Tiles[Levels.columns, Levels.rows];


I can give them xPos, yPos and state( 0 or 1 ):

 for(int j=0; j<row; j++)
         {
             for(int i=0; i<col; i++)
             {
                 Debug.Log(i + " " + j);
                 tilePosition[i, j] = new TileControl.Tiles();
                 tilePosition[i, j].xPos = i;
                 tilePosition[i, j].yPos = j;
                 tilePosition[i, j].state = TileControl.map[j, i];
 
             }
         }


I can test whether they are filled or not:

 int empty = 0;
         int filled = 0;
 
         for(int j=0; j<row; j++)
         {
             for(int i=0; i<col; i++)
             {
                 if(tilePosition[i, j].state == 1) filled++;
                 else empty++;
             }
         }
         Debug.Log("Filled: " + filled + "        Empty: " + empty);
         Debug.Log("Lenght: " + tilePosition.Length);



This means: filled = 9; empty = 3; length = 12;

When I try to get the array's length from somewhere else, it's 0 and boom, ''Array index is out of range'' :(

avatar image Voenskjall · Mar 29, 2015 at 08:07 AM 0
Share

Problem solved, the last reply was a week ago, so I marked your answer.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple spawn points using Array... 1 Answer

How to apply more than one trigger on a single gamebject 1 Answer

[SOLVED]How to use attributes from array of custom class instances? 1 Answer

Checking if a position is occupied in 2D? 1 Answer

Spawning GameObject from class array Javascript 2 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