- Home /
Help with destroying within a certain zone...
I am trying to make a hitbox. So if I hit space while an object is inside my hitbox it will be destroyed. I did a quick search and found this.
It provided some code on how to destroy within a sphere. I modified it and got this:
if(condition == true) {
Collider[] nearObjects = Physics.OverlapSphere(transform.position, 3);
//Return an array of all the colliders within a certain radius of some obj.
foreach (Collider object in nearObjects) {
//Iterate through the array
if(object.tag == "enemy") //Does the object have a certain tag.
Destroy(object.gameObject);
//If yes, then Destroy the gameObject the collider is attached to
}
}
But I am getting all sorts of errors (original version got errors too). Can someone tell me why?
P.S. Errors include:
" ; expected. Insert a semicolon...."
"Unexpecte token:)"
"expecting ), found 'object'" etc.....
Answer by RanRanRu · Nov 06, 2011 at 10:43 AM
Your script is JavaScript right? (.js)
The code you're trying to use is C#, so there are a couple differences in syntax for declaring variables.
C# :
Collider[] nearObjects = .......
foreach (Collider object in nearObjects) .........
JS:
var nearObjects : Collider[] = .........
for(var object:Collider in nearObjects)..........
Your answer
Follow this Question
Related Questions
How to make trigger removes script. 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Destroy trigger so animation only plays once. 1 Answer
Change skybox at runtime? 2 Answers
Getting audio.PlayOneShot to work 2 Answers