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 Dude959 · Apr 15, 2021 at 12:46 PM · positionprogramming

Using Dictionaries to store and retrieve GameObjects?

Ive been working on a isometric grid where buildings are GameObjects stored as values in a dictionary with a vector2 (coordinates) as a key. A newly instantiated object is added into the dictionary with the key of its coordinates under IsOccupied.

 GameObject InstantiatedObject = Instantiate(finalObject, transform.position, Quaternion.identity);
 if(gridDataOccupied.ContainsKey(new Vector2Int(cp.x, cp.y)))
 {
     gridDataOccupied[new Vector2Int(cp.x,cp.y)] = InstantiatedObject;
 }
 else
 {
     gridDataOccupied.Add(new Vector2Int(cp.x,cp.y), InstantiatedObject);
 }

This code takes instantiates selected object (finalObject) and lists it in the grid coordinates in the dictionary at the cursors position (transform.position) onto the nearest grid under the cursor (cp)

 b_brush.GetComponent<Brush>().gridDataOccupied[new Vector2Int(cpb.x +1, cpb.y)].GetComponent<HouseBehaviour>()

This code reaches to a cell beside it and checks if it has the script . This and it works. So i know getting GameObjects from dictionaries work.

 Destroy(b_brush.GetComponent<Brush>().gridDataOccupied[new Vector2Int(cpb.x +1, cpb.y +1)]);

this code destroys the object. It produces no errors, but doesn't do anything.

 b_brush.GetComponent<Brush>().gridDataOccupied[new Vector2Int(cpb.x +1, cpb.y +1)].GetComponent<HouseBehaviour>().Level == Level

the above checks if the house is the same level as a nearby house. It produces no errors also, but doesn't do anything.

 b_brush.GetComponent<Brush>().gridDataOccupied[new Vector2Int(cpb.x +1, cpb.y +1)].GetComponent<HouseBehaviour>().is2x2 == true

Getting the bool from the script as stated above produces an error (i dont know why or what to do about it). So it doesn't work. Im assuming it's not working because its creating a copy of it when i use this code rather than getting the origional. If that's the case, how do i get it to get the origional? Why am I getting an error when I try to get is2x2? Any help would be greatly appreciated as I am a compleate noob at programming and all of this is new to me. Thanks.

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 andrew-lukasik · Apr 15, 2021 at 03:18 PM

Use your gridDataOccupied Dictionary as it's name suggests - as a valid info on grid occupancy. So when filling a cell, always make sure nothing is there already, otherwise you're just replacing that object while loosing it's reference:

 var xy = new Vector2Int( cp.x , cp.y );
 if( gridDataOccupied.ContainsKey(xy) )
 {
     Debug.Log($"Negative. {xy} is occupied",gridDataOccupied[xy]);
     return false;
 }
 else
 {
     GameObject instantiated = Instantiate( finalObject , transform.position , Quaternion.identity );
     gridDataOccupied.Add( xy , instantiated );
     Debug.LogError($"{xy} is occupied by {instantiated.name} now.",instantiated);
     return true;
 }

And, naturally, once you destroy a GameObject - make sure gridDataOccupied is updated on that too:

 var xy = new Vector2Int( cp.x , cp.y );
 if( gridDataOccupied.ContainsKey(xy) )
 {
     var go = gridDataOccupied[xy];
     if( go!=null ) Destroy( go );
     else Debug.LogError($"You messed something up. gridDataOccupied[ {xy} ] is null");
     gridDataOccupied.Remove( xy );
     return true;
 }
 else
 {
     Debug.LogError($"Negative. {xy} is not even occupied.");
     return false;
 }

When it comes to your question. Rewrite this line:

 gridDataOccupied[new Vector2Int(cpb.x +1, cpb.y +1)].GetComponent<HouseBehaviour>().is2x2 == true

As:

 var xy = new Vector2Int( cpb.x+1 , cpb.y+1 );
 var go = gridDataOccupied[xy];
 if( go==null ) Debug.LogWarning($"gameobject under {xy} coordinate is null.");
 var houseComp = go.GetComponent<HouseBehaviour>();
 if( houseComp==null ) Debug.LogWarning($"{go.name} has no HouseBehaviour component",go);
 Debug.Log($"house.is2x2: {houseComp.is2x2} ",go);

And answer surely reveal itself in any Console window nearby.

Comment
Add comment · Show 1 · 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 Dude959 · Apr 16, 2021 at 02:25 AM 0
Share

I debugged and figured out the probolem. The part of my code that handles housing was accessing the prefab rather than the instance because I was assigning the grid finalObject rather than InstantiatedObject. Thanks a bunch for the help.

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

156 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 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

Multiple Cars not working 1 Answer

Camera rotation around player while following. 6 Answers

Does it make any difference whether position is an integer or a float? 2 Answers

Make something arrive at position in exactly X seconds? 2 Answers

How to Set Animation Position on Slope (animation) help please 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