In one script when you make score you get multiple objects to be scoring points and destroying themselves, however when I call this I want one thing to only happen once when my array length is 5, how can I run this code only once while the other code gets ran all the other times? here is the code I’m talking about:
private bool instantiating = false;
public IEnumerator RespawnPieces(float newPositionx, float newPositionz){
while (instantiating)
yield return new WaitForSeconds(0.3f);
instantiating = true;
Vector3 positionToInstantiate = new Vector3(newPositionx, 0, newPositionz);
Collider[] hitColliders;
do{
positionToInstantiate.y = Random.Range(-13, 10);
hitColliders = Physics.OverlapSphere(positionToInstantiate, 1);
} while (hitColliders.Length > 0);
foreach(GameObject mObj in GameObject.FindGameObjectsWithTag("MoleculeHasMatch")){
ObjectsWithMatch = GameObject.FindGameObjectsWithTag("MoleculeHasMatch");
testvariable = ObjectsWithMatch.Length;
print ("Array lenght " + ObjectsWithMatch.Length);
print ("int " + testvariable);
}
if(testvariable == 5){
Instantiate(UMoleCule, positionToInstantiate, Quaternion.identity);
} else {
Instantiate(MoleCule, positionToInstantiate, Quaternion.identity);
}
instantiating = false;
}
Part I want happening only ONCE out of the times it gets called:
if(testvariable == 5){
Instantiate(UMoleCule, positionToInstantiate, Quaternion.identity);
}
This is exactly what I tried before asking however when I did this it didn't do anything because it's being called on the same frame each time so it doesn't matter to the code because the boolean will either be true or false every single time.
– Jan_JuliusAre the calls on the same instance? If so my method will work. If not my method will still work, you just need to make the bool static or put it on a manager class all the instances can access.
– KiwasiYeah but if I hvae 5 of the mcalling that at the same time what happens is that they will all set the boolean to something at the same time, run it at the same time and it will happen 5 times anyways? I want it to happen one out of 5 times basically.
– Jan_Julius