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 BenoitMenard · Jun 03, 2021 at 04:26 AM · 2d array

Searching exclusively for specific indexes in a array

I'm having troubles finalizing my battle system. Currently its a direct single target, you make your move, they do the same, but that was mostly for testing of other components.

In the end, i would like there to be a 3x3 grid stored in a array. Where enemy's spawn at random on the board. This I can manage.


So just for explanatory reasons lets say bottom left is box 0, middle left is box 3, top left 6 and finishing with the 8th square top right.


Let's say I have a spell that should target in a X formation. This would affect grid locations 0,2,4,6,8. So the first course of action would be verifying if there is a gameobject(enemy) present in only those specific locations.


/ I understand checking through the entire grid array if any of locations are taken. But not how to call upon a specific list of indexes in that array. /

And if so, call the damage method on to those gameobjects. Which is already prepared.


But I can't seem to figure out the targeting system I would like to achieve and then checking if it hits anything.

Any kind of assistance or guidance would be greatly appreciated!

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 Llama_w_2Ls · Jun 03, 2021 at 07:40 AM

You should probably use a dictionary with keys of type int - for the grid square index - and values of type game object - for the object at that square. Then, you can create an array of indexes you wish to search through, and check if a game object is at that position. For example:

 // ===== Dictionary Initialization =====
 
 var gridDict = new Dictionary<int, GameObject>();
 
 for (int i = 0; i < 9; i++)
 {
      gridDict.Add(i, null);
 }
 
 // ===== Enemy Spawning =====
 
 int randomEnemyIndex = Random.Range(0, 9);
 var enemyObj = Instantiate(Enemy, enemySpawnPos, Quaternion.identity);
 
 // Places enemy GO at a random grid position
 gridDict[randomEnemyIndex] = enemyObj;
 
 // ===== Cross Attack =====
 
 var crossAttack = new int[] {0, 2, 4, 6, 8};
 
 foreach (var index in crossAttack)
 {
      // There's an enemy at one of the cross attack positions
      if (gridDict[index] != null)
      {
           // Destroys enemy
           Destroy(gridDict[index]);
           break;
      }
 }

Hope that helps @BenoitMenard

Comment
Add comment · Show 2 · 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 BenoitMenard · Jun 03, 2021 at 02:01 PM 0
Share

That's looks great. Thank you very much.

That being said I understand the majority. How we are spawning, attacking and damaging/destroying.

However I'm not too familiar with dictionaries so in the initialization for example. Most of the game is UI based. So I'm going to have a panel with the 9 possible gameobject spaces/images.

When I'm initializing the dictionary, would I not need a:

[Serializablefield] GameObject[] Board;

So that I can specify what the actual pieces are for reference. As I have a whole UI layout that's ready and scaled for any resolution. (Building for mobile)

I don't feel like I can achieve that by populating the grid inside the script. But probably I'm wrong.

avatar image Llama_w_2Ls BenoitMenard · Jun 03, 2021 at 05:24 PM 0
Share

Yes, you would need a game object array if you wanted to set all the values through the inspector. You can still populate the grid through the new method by looping through all the objects and adding them to the dictionary. I don't understand your new problem.

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

118 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

Related Questions

Hex map movement problem 2 Answers

NullReferenceException with MultiDimensional Array 0 Answers

2D Hex/ Square Tile Resource Controller Help 0 Answers

Custom Editor Dynamic 2D array 1 Answer

werid array behavior 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