- Home /
Foreach statement issue with UnityEngine.Collider
Hello,
I am very new at Unity and programming in general so please forgive my lack very basic understanding. I am currently still on the video tutorial stages! I have created a simple arcade game, I am trying to create an explosive force at the point where an enemy collides with the player. Here is the code provided with the video tutorial I am using.
foreach(Collider hit in collider){
if(!hit){
continue;
}
if(hit.rigidbody){
hit.rigidbody.AddExplosionForce(200.0f, enemy_position, 100.0f);
explosion.Emit ();
}
}
When I use this code the following error is returned in Unity:
"Assets/Player/PlayerStat.cs(22,9): error CS1579: foreach statement cannot operate on variables of type UnityEngine.Collider' because it does not contain a definition for GetEnumerator' or is not accessible"
I would really appreciate any help with this issue as I have been unable to find a solution elsewhere online. Thanks, and let me know if you need to know any more information to help me solve this issue. I have only provided the code that I think the issue is related to.
Thanks in advance
-Jack.
EDIT: It's executing in public void OnTriggerEnter(Collider collider)
It's executing in public void OnTriggerEnter(Collider collider)
Answer by Dave-Carlile · Apr 21, 2013 at 01:50 PM
Where is your code executing? In OnCollisionEnter?
The documentation has a good example of iterating through collision points.
The error message you're getting is basically stating that your collider variable is of type Collider, and the Collider class isn't a list, so you can't iterate through it.
Generally collisions are handled in OnCollisionEnter which gives you information about the contact points. Another collision related method is OnTriggerEnter, but that only notifies you that a collision occurred and doesn't provide any information about contact points.
It's executing in public void OnTriggerEnter(Collider collider)
Okay, then the collider is a single object that entered the trigger. There is no need to loop through anything on it.
Your answer
Follow this Question
Related Questions
How to get the colliders working? 1 Answer
Multiple Cars not working 1 Answer
I have to set a Collider[] to Collider - what do? 1 Answer
Distribute terrain in zones 3 Answers
Can't get a laser working properly. 2 Answers