how to destroy an item after collision - unity3d

I am creating an fps game, I have created the gun, the bullet, and the enemy.
Now I want to make my enemy destroyed after the collision with bullet.
My enemy is a gameobject named Fire and tagged Enemy and my bullet named "Cube 1(Clone)" and tagged "Cube 1(Clone)". I made a script for that:
#pragma strict
function OnTriggerEnter(theCollision : Collider)
{
if(theCollision.gameObject.name=="Cube 1")
{
Destroy(gameObject);
Debug.Log("Dead");
}
}
But it does not work.

You need to check the tag not the name. You could check for name but remember it will have "(Clone)".
function OnTriggerEnter(theCollision : Collider)
{
if(theCollision.tag == "Cube 1")
{
Destroy(gameObject);
Debug.Log("Dead");
}
}
If you are not sure that you tagged correctly, you can simply use both checks in your if statement.
if(theCollision.tag == "Cube 1" || theCollision.gameObject.name == "Cube 1(Clone)")
Destroy(gameObject);

Well since the bullet is tagged Cube 1(Clone) , I would use
if(theCollision.tag == "Cube 1(Clone)"){...}
And would probably would rename the tag to something meaningful, say bullet.

Related

how do i make sword work with enemy collider

here is my current code:
public GameObject enemy;
void OnCollisionEnter(UnityEngine.Collision collisionInfo)
{
if (collisionInfo.collider.tag == "sword")
{
Debug.Log("works");
enemy.SetActive(false);
}
else
{
Debug.Log("doesnt work");
}
}
i have attached this to the enemy and also i tried a different script attached to the sword
void OnTriggerStay(Collider col)
{
if (Input.GetButtonDown("Fire1"))
{
if (col.GetComponent<Collider>().tag == "enemy")
{
Destroy(col.gameObject);
}
}
}
both codes don't work, it seems that the problem isnt with the sword collision cus i have also added the tag to another gameobject and it doesnt work. i looked online but havent found anything that works so fat
update: it seems that i have forgot you need a rigid body for collision detection. simple mistake but it made my code not work!
For collision enter to work, both the sword and the enemy colliders need to have the "IsTrigger" box unticked.
For on triggerstay is the opposite, both need to have that box ticked.
Basically, Triggers do not collide they call that OnTriggerEnter function, while non-triggers use the OnCollisionEnter.

Can't detect collision with child and ground

I have the below script attached to the ground game object to detect collisions from my players' child objects but for some reason, the collisions are not detecting.
My player (parent, empty game object) has a Rigidbody and Jump script attached to it, meanwhile, the child game objects (body & feet) have just box colliders on them.
Would love to know why this isn't working :)
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Body")
{
Debug.Log("Game over!");
}
else if (collision.gameObject.tag == "Feet")
{
Debug.Log("Alive!");
}
}
For some reason, it works when you do collision.collider.tag instead of collision.gameobject.tag, like so:
if(collision.collider.tag == "Head")
{
Debug.Log("Game over!");
}
Not exactly sure why this works, but it works! Defo going to look into this a bit more.

Why is "Colls" an empty Array?

I try to judge a player's approach to a monster using "OverlapSphere."
But somehow it doesn't work the way I want it to.
Is there anything wrong with the code below?
bool IsPlayerNear()
{
Collider[] colls = Physics.OverlapSphere(transform.position, 3f);
foreach(Collider hit in colls)
{
if(hit.gameObject.tag == "Player")
return true;
}
return false;
}
The player character has a capsule collider and "Player" tag.
But the "colls" array contains nothing.
A frequent cause of this sort of problem is that the capsule collider is attached to a child or parent of the tagged gameobject, so maybe check that, your code looks OK to me.

Compare tags in UnityScript

We are building a 3D game and right now I am stuck on an issue where I need to compare multiple tags to activate or deactivate a trigger that sends the player back to the respawn position.
This is currently the code:
#pragma strict
var spawnPoint : GameObject;
function OnTriggerStay ( other : Collider )
{
if (other.tag == "Player" && other.tag != "Ball")
{
other.tag == "Player";
other.gameObject.transform.position = spawnPoint.transform.position;
}
else if ( other.tag == "Ball" && other.tag == "Player" )
{
}
}
I am uncertain how to fix this to do the following:
If the player touches the trigger without there being a ball colliding with it, the player respawns. This is to create the feeling that the particles kill you.
If the player touches the trigger when there is a ball colliding with it as well, nothing happens to the player and so the player can freely pass through.
What we want to do is that we want to push a ball over a geyser so it covers it and if the geyser is not covered and the player tries to pass over it, the player respawns.
We also tried with another code and while it allows the ball to pass, it does not allow the player to do so. Even after the ball has been placed.
#pragma strict
var spawnPoint : GameObject;
function OnTriggerStay ( other : Collider )
{
if (other.tag == "Ball")
{
other.enabled = false;
}
else if (other.tag == "Player")
{
other.gameObject.transform.position = spawnPoint.transform.position;
}
}
So, I believe your problem is, when you use that function, you are only checking the first collision. So whats happening is your getting a value of either the player or the ball, not both. You need to store all the collision so you can compare all of them. To do that you can follow the generals of this documentation. http://docs.unity3d.com/ScriptReference/Collision-contacts.html
It's talking about collisions, but the same general principle should apply.
Hope this helps!

Why is my object not find other Gameobject name?

I want to destroy my object when it will enter in OnTriggerEnter method.
I am using something similar this code
void OnTriggerEnter(Collider C)
{
if(C.name == "GameObject Name")
{
Destroy(gameObject);
Time.timeScale=0;
}
}
my object enter into OntriggerEnter event but it can't find C.name == "GameObject Name" .
Sorry friends, now one problem is occured..actually my object is not detect collider gameobject.I mean my object is not enter in trigger event. .now What can I do?
Or you can use Tag option on the object. Right corner of the Unity choose Tag 'Player' From Inspector and apply this code[C#].
void OnTriggerEnter(Collider collision){
if (collision.gameObject.tag == "Player") {
Destroy(Object_name_to_destroy);
Time.timeScale=0;
}
}
You want the name of the GameObject belonging to the Collider, so use C.gameObject.name.
Sorry this is super late but for someone facing this issue in future...try checking for blank spaces after your game object name i.e "example_name " is different from "example_name"