- Home /
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
              
 
               
              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);
             }
         } 
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                