The question is answered, right answer was accepted
Collision killing all enemies not the one the player collides with
As title says killing all enemies with tag enemy , all in Java
Fly (enemy) Collision Code:
#pragma strict
function OnCollisionEnter2D(collision2D: Collision2D ){
var gameManager = GameObject.FindGameObjectWithTag("GameController");
//var player = GameObject.FindGameObjectWithTag("Player");
//var fly = GameObject.FindGameObjectWithTag("Fly");
if (collision2D.gameObject.tag == "Player"){
gameManager.SendMessage ("DeathFly");
}
}
GameManager code : (only the parts for the enemies as its a long script)
#pragma strict
var fly : GameObject;
function DeathFly(){
fly = GameObject.FindGameObjectWithTag("Enemy");
fly.SendMessage("Kill");
}
Fly (enemy) Code :
#pragma strict
var damage : int = 10;
var health : int = 10;
var hitSound : AudioClip;
var flySound : AudioClip;
var deathSound : AudioClip;
var Boost : GameObject;
var healthBar : UI.Slider;
function Update(){
var audio : AudioSource = GetComponent.<AudioSource>();
if(health == 0){
AudioSource.PlayClipAtPoint(deathSound, Camera.main.transform.position);
Kill();
}
healthBar.value = health;
}
function Kill(){
Boost = Instantiate(Resources.Load("Prefabs/Cloud"), transform.position, transform.rotation) as GameObject;
Destroy(this.gameObject);
}
function ApplyDamage(){
health = health -= damage;
}
if anyone could help with killing when landing on the head i would be very great full.
Answer by Adam-Halley-Prinable · Jan 04, 2016 at 01:25 PM
Try replacing the fly collision code with this:
function OnCollisionEnter2D(collision2D: Collision2D ){
if (collision2D.gameObject.tag == "Player"){
GetComponent<INSERT_NAME_OF_FLY_CODE_FILE_HERE>().Kill();
}
}
And in the fly code, add the word "public" in front of "function Kill(){"
This is assuming the fly collision code and the fly logic code are both attached to the same fly game object.
Answer by Aleg8r · Jan 04, 2016 at 02:21 PM
Thanks SOOO much been messing with this for hours
No worries :) Choose me as the accepted answer? Gotta get dat sweet sweet reputation!
Follow this Question
Related Questions
How do I use arrays, with getcomponent? 2 Answers
BCE0043, BCE0044 and UCE0001 errors after upgrade from 4.3 to 5.3 0 Answers
Save system 1 Answer
GameObject, Select Parent As Object 0 Answers
Writing save files (JavaScript) 0 Answers