IEnumerator processTrade(int rid1, int rid2, float ratio, AgentScript otherAgent) { //First we need to calculate how much of each resource can be traded. while (resources[rid2] >= 1f && otherAgent.resources[rid1] >= ratio) { if (otherAgent.dead) { break; // If they die during the exchange, continue on like nothing happened } resources[rid1] += ratio; // Gain ratio resources resources[rid2] -= 1f; // Lose 1 resource otherAgent.resources[rid1] -= ratio; //Other agent loses ratio otherAgent.resources[rid2] += 1f; //Other agent gains 1 processResourceChanges(); //Updates health for both agents otherAgent.processResourceChanges(); yield return(new WaitForSeconds(0.1f)); } otherAgent.endTrade(); //End the trade endTrade(); }