enemy wont dissapear when tagged

Hi guys when my player hit my enemy or jumps on him i want the enemy to disappear, the enemy has been tagged but when i run my game it will not disappear can you see what is wrong, i have declared var collide to false, if anyone can see what i am doing wrong. Should i be using OnControllerCollider instead of OnTrigger as this is for events and not for collision detection: :wink:

the code is attached to my player

var collide = false;

function OnTriggerEnter(collide : Collider)
{
	// Check for enemies via a tag 
	if(collide.gameObject.tag == "enemy")	
{
	return true;
}

}

Okay i have changed the code to the following and it have issued a print statement and its saying its collided but will not disappear, i ave tried defining colide as false and then return true when i hit the enemy but its not working a bit stuck - my updated code, also the enemy goes straight through the player, confused:

var collide = false;

function OnControllerColliderHit(collide : ControllerColliderHit)
{
	// Check for enemies via a tag 
	if(collide.gameObject.tag == "enemy")	
{
	print ("you have hit the enemy");
	return true;
}

}

Where is the code that tells your enemy to disappear?

Wait, I think I see what you’re doing.

That “collide” at the top is a boolean.

The “collide” that is a parameter in “OnControllerColliderHit” is not the same as the bool collide up above it. It’s a totally different thing.

So if you’re returning true, you’re not actually changing the “collide” boolean to true, you’re just returning “true” to nothing.

After that print statement, do this:

this.collide=true

I’m assuming that when “this.collide==true”, then something will happen in another script.

Alternatively, name one of the variables something else so that you can tell them apart.

To disappear your colliding gameobject you can use: Destroy (gameObject); I suggest you to print the collide.gameObject.tag anyways to see where the collision is failing… To see more about the destroy function: http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html

His question isn’t very clear, he says “Dissapear” perhaps he just wants it to go invisible and not destroy itself, in order to do that you would basically remove the mesh from the object.

Well it’s true, you’re right if he needs that he may only uncheck the mesh renderer instead.

Hi Owiz,devprincess,LPGaming-Company,

i thought the variable collide was the same as where ControllerColliderHit was, so if i was to change it to say hit and then true would this work along with destroying the gameobject please see below guys, or could i just .this.collide=true in place of destroying the object would that work:

var collide = false; 

function OnControllerColliderHit(hit : ControllerColliderHit)
{


   // Check for enemies via a tag 


    if(hit.gameObject.tag == "enemy")   
{

   Destroy (gameObject);


   return true;

}



}

Hi airesdav, maybe you can try this way:

function OnControllerColliderHit(hit : ControllerColliderHit)

{

 //Print the value of the collider:
Debug.Log("Colliding gameobject tag name:"+ hit.gameObject.tag);
 
   // Check for enemies via a tag 


    if(hit.gameObject.tag == "enemy")   

{
   //Destroy my gameobject instance... (won't use it anymore)
   Destroy (gameObject.FindWithTag("enemy"));

}


}

Anyway, if what you meant for “disappear” is only to get off the render mesh of the gameobject, you should go this way:

function OnControllerColliderHit(hit : ControllerColliderHit)

{

 //Print the value of the collider:
Debug.Log("Colliding gameobject tag name:"+ hit.gameObject.tag);
 
   // Check for enemies via a tag 


    if(hit.gameObject.tag == "enemy")   

{
   //Please do not destroy my gameobject, as I will use it again one more time maybe... so here I'm just making its mesh invisible
   gameObject.FindWithTag("enemy").renderer.enabled=false;

}


}

Hope it helps anyway!

ughhh i hate unityscript…

to destroy the object you have hit

 var hasCollidedWithEnemy : false;

function OnTriggerEnter(collide : Collider){

    // Check for enemies via a tag 

    if(collide.gameObject.tag == "enemy")   {
        Destroy(collide.gameObject);
        hasCollidedWithEnemy = true;
    }

 

}

of you want to hide it, then as shown above.

 var hasCollidedWithEnemy : false;
 var ObjectRenderers;

function OnTriggerEnter(collide : Collider){

    // Check for enemies via a tag 

    if(collide.gameObject.tag == "enemy")   {
	ObjectRenders = collide.gameObject.GetComponentsInChildren<Renderer>(false);
					for (var i: 0; i < ObjectRenders.Length; i++){
						Renderer renderer = ObjectRenders[i];
						renderer.enabled = false;
			}
        hasCollidedWithEnemy = true;
    }

 

}

The above code is psuedo so dont rape me if it doesnt work, and i hate unity script.

Hi guys,

Thanks for all the feedback it all really helps me out here big time so this is the code I have written and has managed to make the enemy disappear what I need to know how I get and explosion when I have collided with my object:

private var killenemy = false;

function OnControllerColliderHit(hit : ControllerColliderHit)
{
	if(hit.gameObject.tag == "falloff")  // fall of platform
	{
		dead = true;  // he is dead 	
	}
	else if (hit.gameObject.tag == "enemy")
{
	Destroy(hit.gameObject); // remove from the scene 
	killenemy = true;	
}

}

kind regards
;-):sunglasses: