Question by
mertmuslum20 · Jan 16 at 01:03 PM ·
listforeach
"InvalidOperationException Collection was modified" in a foreach loop
void ShotGun ()
{
GunAnimSet.Play("Shoot", -1, 0);
Muzzle.Play();
SesKaynak.Play();
int i = 0;
foreach(Quaternion quat in pellets) // <-- Error here
{
pellets[i] = Random.rotation;
GameObject p = Instantiate(pellet, BarrelExit.position, BarrelExit.rotation);
p.transform.rotation = Quaternion.RotateTowards(p.transform.rotation, pellets[i], spreadAngle);
p.GetComponent<Rigidbody>().AddForce(p.transform.right * pelletFireVel);
i++;
}
}
Comment
int numPellets = pellets.Length;// "Count" when pellets is a list
for( int i=0 ; i<numPellets ; i++ )
{
pellets[i] = Random.rotation;
GameObject instance = Instantiate( pellet , BarrelExit.position , BarrelExit.rotation );
instance.transform.rotation = Quaternion.RotateTowards( instance.transform.rotation , pellets[i] , spreadAngle );
instance.GetComponent<Rigidbody>().AddForce( instance.transform.right * pelletFireVel );
}
Your answer
Follow this Question
Related Questions
c# foreach does not work 0 Answers
c# foreach does not work 1 Answer
can't save values from another class to a list 1 Answer
Playing Through a Queue/List of Audio Sources, One at a Time 1 Answer