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 VerrucaG · Feb 15, 2015 at 09:02 PM · errorenumhashtable

Hashtable.Enumerator: snapshot out of sync

Hi all, I've been getting the following exception: Hashtable.Enumerator: snapshot out of sync which I believe is being caused by attempting to read and write to a hashtable at the same time.

Can someone give me some advice on the best way of handling read/writing for hashtables at the same time?

Here's the code I'm using in case I've got this wrong:

 #pragma strict
 private var targetPosition: Vector3;
 private var forceDirection: Vector3;
 private var distanceFromCentre: float = 0.0;
 private var targetMass: float = 0.0;
 private var gravitationalForce: float;
 private var localBodyTable = new Hashtable();
 private var localMassTable = new Hashtable();
 
 function Start () {
      if(!StellarIndex.bodyTable.Contains(transform.parent.name))
     {
         StellarIndex.bodyTable.Add(transform.parent.name, transform.position);
     }    
     if(!StellarIndex.massTable.Contains(transform.parent.name))
     {
         StellarIndex.massTable.Add(transform.parent.name, rigidbody.mass);
     }
 }
 
 function FixedUpdate () {
     
     // If this hashtable is populated (implicitly the hashtable for mass is also populated)
     if(StellarIndex.bodyTable.Count > 0)
     {
         // Cycle through the hash table keys and find the associated mass / vector3 values for each object
         for each(var i: String in StellarIndex.bodyTable.Keys)
         {    
                 targetPosition = StellarIndex.bodyTable[i];
                 forceDirection = targetPosition - transform.position;
                 targetMass = StellarIndex.massTable[i];
                 distanceFromCentre = Vector3.Distance(targetPosition, transform.position);
                 gravitationalForce = GameLogic.gravitationalConstant * ((rigidbody.mass * targetMass) / distanceFromCentre);
                 rigidbody.AddForce(forceDirection * gravitationalForce * Time.deltaTime);
         }
     }
 }
 
 function LateUpdate () {
     
     //Update every hashtable entry with new data    
     for (var i: String in StellarIndex.bodyTable.Keys)
     {    
         StellarIndex.bodyTable[transform.parent.name] = transform.position;
     }    
     for(var p: String in StellarIndex.massTable.Keys)
     {    
         StellarIndex.massTable[transform.parent.name] = rigidbody.mass;    
     }        
         
     
 }    
 

Any help with this would be greatly appreciated :-) 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
0

Answer by VerrucaG · Feb 16, 2015 at 08:40 PM

So I managed to solve this. It seems that the loops in the LateUpdate function were to blame. My LateUpdate function now looks as such:

  function LateUpdate () {
      
      //Update every hashtable entry with new data      
      StellarIndex.bodyTable[transform.parent.name] = transform.position;
      StellarIndex.massTable[transform.parent.name] = rigidbody.mass;            
  } 

As a side note my gravitational force variable was occasionally outputting values of Nan or Infinity which I have clamped with an if loop like so:

         if(!float.IsNaN(gravitationalForce) && !float.IsNaN(forceDirection.x) && !float.IsNaN(forceDirection.y) && !float.IsNaN(forceDirection.z))
         { 
             if(!float.IsInfinity(gravitationalForce) && !float.IsInfinity(forceDirection.x) && !float.IsInfinity(forceDirection.y) && !float.IsInfinity(forceDirection.z))
             {
                 rigidbody.AddForce(forceDirection * gravitationalForce * Time.deltaTime);
                 //Debug.Log(transform.parent.name + " is pulled towards" + i + " by " + gravitationalForce);
             }
         } 
Comment
Add comment · 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

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

2 People are following this question.

avatar image avatar image

Related Questions

hashtable: snapshot out of sync 2 Answers

Error with Structs and Enums 2 Answers

Hashtable problem c# 1 Answer

[CLOSED] UnityScript - enum to numeric value error 1 Answer

Unity enum map - how to make it more efficient/clean? Linear line complexity. Best practices. 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