Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 nitrogames79 · Jan 24, 2020 at 08:37 PM · valuekeydictionary

Dictionary wont give me value with correct key

No matter what I try GetBlock always has neochunk return null, despite neochunk equaling the WorldPos put into the chunks Dictonary. For ex one of the chunks has a key of (0, 0, 4), but even when newpos is (0, 0, 4) neochunk is null.

 public Matrix4x4 id = Matrix4x4.identity;
 
     Chunk chunk;
     WorldPos pos;
 
     public Material material;
 
     public int ViewDist = 4;
 
     FindChunk chunkHelp;
 
     private int chunksize = Chunk.chunksize;
 
     public Dictionary<WorldPos, Chunk> chunks = new Dictionary<WorldPos, Chunk>();
     private void Start() {
         chunkHelp = new FindChunk();
         for (int x = 0; x < ViewDist; x++){
             for (int y = 0; y < ViewDist; y++){
                 for (int z = 0; z < ViewDist; z++){
                     chunk = new Chunk(new WorldPos(x * chunksize, y * chunksize, z * chunksize), this);
 
                     chunk.GenerateChunkArray();
 
                     chunks.Add(chunk.pos, chunk);
                 }
             }
         }
         foreach(WorldPos pos in chunks.Keys){
             Debug.Log(pos.x);
             Debug.Log(pos.y);
             Debug.Log(pos.z);
             Debug.Log("Next");
         }
         foreach(Chunk chunk in chunks.Values){
             chunk.GetMesh();
         }
     }
 
     private void Update() {
         foreach(Chunk chunk in chunks.Values){
             Graphics.DrawMesh(chunk.mesh, id, material, 0); 
         }
     }
 
     public Blocks GetBlock(WorldPos pos, int x, int y, int z) {
         WorldPos newpos = chunkHelp.ChunkToChunk(pos, x, y, z);
         Chunk neochunk;
         chunks.TryGetValue(newpos, out neochunk);
 
         if (neochunk == null){
             Debug.Log(newpos.x);
             Debug.Log(newpos.y);
             Debug.Log(newpos.z);
             Debug.Log("Null chunk spot");
             return Blocks.Air;
         }
 
         WorldPos nnpos = chunkHelp.ChunkToBlock(x, y, z);
 
         return neochunk.blocks[nnpos.x, nnpos.y, nnpos.z];
     }




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
2

Answer by Esteem · Jan 24, 2020 at 11:40 PM

  1. why don't you use Vector3 instead of WorldPos

  2. If WorldPos coordinate values are floats, don't just debug them, you;re rounding the value. Float values can't be trusted when used with dictionaries like this.

    do a Debug.Log(newpos.x.ToString("0.000000000000000"); to see if the value is actualy what you need it to be
    if it's a float then sometimes newPos.x can be 5.99999999999999996 or whatnot instead of 6

Comment
Add comment · Show 4 · 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 nitrogames79 · Jan 25, 2020 at 12:57 AM 0
Share

The x, y, and z of WorldPos are all ints, so it shouldn't end up like that.

avatar image Bunny83 nitrogames79 · Jan 25, 2020 at 03:21 AM 0
Share

What exactly is WorldPos ? A struct? If it's not a struct, how do you ensure equality? Did you proberly implement GetHashCode and Equals in your custom type? Since you use a custom type as key in a dictionary, that custom type is the most important information. All the other code you've posted is pretty irrelevant.

avatar image nitrogames79 Bunny83 · Jan 25, 2020 at 04:00 AM 0
Share

I feel real dumb, but the problem was that WorldPos was a class ins$$anonymous$$d of a struct.

Show more comments

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

121 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

Related Questions

get key of item from dictionary based on value c# 1 Answer

Changing a dictionary value from another script 1 Answer

I am very new to programing > I have a script attached to a cloned game-object and need help referancing ..... Any help would be wonderful ^_^ 3 Answers

Dictionary doesn't exist? 1 Answer

Default Null Value for Key Not Found in Dictionary? 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