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*;*
-
forceDirection = targetPosition - transform.position;*
_ targetMass = StellarIndex.massTable*;_
_ 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