Thank you Priceap, you’re absolutely right, it was me writing wrong code. I need to have the coins popping out randomly along the x axis, so my code for this was
coinEmit=transform.Find("slot_coinemitter");
coinPos=coinEmit.transform.position;
for (i=0;i<coinNum;i++){
yield WaitForSeconds (0.05);
var randomPosition = Vector3(Random.Range(-0.2, 0.27), coinPos.y, coinPos.z);
var randomRotation = Quaternion.Euler(Random.Range(0, 360) , Random.Range(0, 0) , Random.Range(0, 90));
slotPayout-=payVar;
textpaid.text=slotPayout.ToString();
var coin = Instantiate(coinPrefab, randomPosition, randomRotation);
etc
The bad line was
var randomPosition = Vector3(Random.Range(-0.2, 0.27), coinPos.y, coinPos.z);
because that means that the coins would always fall out at the position of the original slot machine’s coin emitter, I’m guessing.
It should have been
var randomPosition = Vector3(Random.Range(coinPos.x-0.2, coinPos.x+0.27), coinPos.y, coinPos.z);
I realise that I have another problem, namely when I press the ‘Cash In’ button EVERY coin emitter spits out coins and this was componunding the first problem. To check when the user presses a button on the slot machine, I use a raycast from the main camera
function Update(){
var hit : RaycastHit;
if(Input.GetButtonDown("Fire1")) {
var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, hit)){
var hitCollide = hit.collider.gameObject.name;
switch(hitCollide){
case ("slot_button01"):
buttonDown(hitCollide);
cashIn();
break;
case ("slot_button02"):
buttonDown(hitCollide);
placeBet(1);
break;
case ("slot_button03"):
buttonDown(hitCollide);
placeBet(3);
break;
case ("slot_button04"):
spinReels();
buttonDown(hitCollide);
break;
case ("slot_coin_insert"):
insertCoin(hitCollide);
break;
case ("slot_arm"):
moveArmDown();
break;
}
}
}
}
No matter which slot machine I’m using, pressing the ‘Cash In’ button calls the cashIn() function on every slot machine in the scene. I’m thinking the problem lies with
var hitCollide = hit.collider.gameObject.name;