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 /
  • Help Room /
avatar image
0
Question by BigGiantHead · Mar 30, 2017 at 11:58 AM · gameobjectslists

Problem with Dictionary.Remove

Hi all!

I have a Dictionary parameter in my script with Vector3 type of keys and GameObject values.

Vector3s are used to keep track of the positions of a corresponding GameObject in a scene.

The problem comes when I try to remove the gameobject (and the key). Sometimes the Remove() method doesn't find the corresponding key even though the value of the key in Dictionary and of the corresponding parameter is the same.

I try to remove the key-value pair like this:

public void markDestroyedBallPosition (Vector3 posDestroy) if (ballsInScene.Remove(posDestroy)) { removedBallPositions.Add(reducedBalls, posDestroy); %|-172957788_3|% } else { }

I have debugged this and the value of the posDestroy parameter corresponds to the Key in the ballsInScene Dictionary.

If someone from the community could help, I would appreciate it a lot. I am totally stuck with this bug.

Thanks in advance! - Jukka

Comment
Add comment · Show 1
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 BigGiantHead · Mar 30, 2017 at 12:03 PM 0
Share

There is something wrong with the code-section. For some reason the system puts some weird character-lines in the code-section.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SohailBukhari · Mar 30, 2017 at 01:30 PM

@username

Are you trying to remove a single value or all matching values? The reason you don't get a key back when querying on values is because the dictionary could contain multiple keys paired with the specified value. I wrote this script and i tested value removes using specified key, use RemoveByValue() to remove value against your key.

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 
 public class NewBehaviourScript : MonoBehaviour
 {
 
     private Dictionary<Vector3, GameObject> _dic = new Dictionary<Vector3, GameObject>();
     public GameObject value;
 
     // Use this for initialization
     void Start () {
        _dic.Add(Vector3.back, value);
 
         GameObject someValue = value;
         RemoveByValue<Vector3,GameObject>(_dic, someValue);
        
     }
 
     private  void RemoveByValue<TKey, TValue>(Dictionary<TKey, TValue> _dic, TValue someValue)
     {
         List<TKey> itemsToRemove = (from pair in _dic where pair.Value.Equals(someValue) select pair.Key).ToList();
 
         foreach (TKey item in itemsToRemove)
         {
             Debug.Log(item.ToString());
             _dic.Remove(item);
         }
     }
 }

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 BigGiantHead · Mar 31, 2017 at 03:04 PM 0
Share

Thanks for the input, but there isn't duplicate $$anonymous$$ey entries in the Dictionary.

Somewhere I remembered that comparing Vector3s (which the Remove() does in my case) isn't very good thing. I searched how other people have circulated this and I found a post about the problem (this).

However, in my case, comparing two Vector3s in a separate function is impossible because I would need first to get the $$anonymous$$ey based on the value of the other Vector3.

Only other option which I can think of would be using the Value and delete the entry based on that. The problem with this method is that all the GameObjects are with the same name in the scene so I don't know would Unity recognize different gameobjects from each other...

avatar image BigGiantHead · Mar 31, 2017 at 03:16 PM 0
Share

I'm too noob to understand how does your method work....:D

'private void RemoveByValue(Dictionary _dic, TValue someValue)'

This has too many variable with the same name so that I would understand what are passed as parameters to the method and which the method creates itself.

Please 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

97 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

Related Questions

Trying to get gameobject from one list to follow gameobjects in another list 1 Answer

Problem with reading values from scripts on GameObjects in Lists 1 Answer

Trying to program two buttons to appear when the player in my game dies 0 Answers

List resulting in out of range 0 Answers

Finding an object's list index via the object's attached script 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