OnTriggerEnter not working - unity3d

public var enemy:GameObject;
enemy = GameObject.FindGameObjectWithTag("enemy");
function OnTriggerEnter(other:Collider)
{
if(other.gameObject.tag == "enemy")
{
Debug.Log("Dead");
Destroy(gameObject);
}
}
This script is attached to a prefab arrow that gets instantiated. The enemy has a circle collider and the arrow has a box collider. The arrow has on IsTrigger checked. What have I done wrong? Both gameobjects have a rigidbobdy2D attached.

If you use the 2D physics engine, you need to use the 2D functions:
function OnTriggerEnter2D(other: Collider2D)
{
if(other.tag == "enemy")
{
Debug.Log("Dead");
Destroy(gameObject);
}
}

Related

how to fix OnTriggerEnter falling behind of OnCollisionEnter?

I am trying to shoot a projectile and damage all of the blocks in a spherical area.
Bullet object has a fitting size box collider and a bigger shpere collider(isTrigger selected).
Here is my Bullet script for detecting objects in sphere collider area and detonating once it hits to a block.
`
List<GameObject> blocksInDistance;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("block_big"))
{
if (!blocksInDistance.Contains(other.gameObject))
{
blocksInDistance.Add(other.gameObject);
}
}
}
private void OnTriggerExit(Collider other)
{
if (other.CompareTag("block_big"))
{
if (blocksInDistance.Contains(other.gameObject))
{
blocksInDistance.Remove(other.gameObject);
}
}
}
private void OnCollisionEnter(Collision collision)
{
if (isActive)
{
if (collision.gameObject.CompareTag("block_big"))
{
Debug.Log("bullet collision detection");
explode();
deliverDamage();
rb.useGravity = true;
isActive = false;
}
}
}
void deliverDamage()
{
foreach (GameObject block in blocksInDistance)
{
block.GetComponent<Block>().takeDamage(damage);
}
}
`
Almost half of the time ontrigger enter falls behind and oncollision enter gets called first and makes bullet explode but since OnTriggerEnter was never called before explosion none of the blocks gets damaged. How can i give more priority to the Sphere Collider or fix it in a different way?

How to access the collider attached to the children of a prefab?

I have attached a script to a prefab and the script is:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class destroyer: MonoBehaviour {
Circles circles;
Collider2D collider1;
Collider2D collider2;
private void Start() {
collider1 = gameObject.transform.GetChild(0).GetComponent < Collider2D > ();
collider2 = gameObject.transform.GetChild(1).GetComponent < Collider2D > ();
circles = FindObjectOfType < Circles > ();
}
private void Update() {
if (transform.position.y < 2) {
Destroy(gameObject);
circles.instantiator();
}
}
void OnTriggerStay2D(Collider2D other) {
if (collider1.bounds.Contains(other.bounds.max) && collider1.bounds.Contains(other.bounds.min)) {
print("2");
if (other.bounds.Contains(collider2.bounds.max) && other.bounds.Contains(collider2.bounds.min)) {
print("3");
if (transform.position.y > 3) {
print("4");
Destroy(other.gameObject);
Destroy(gameObject);
circles.instantiator();
}
}
}
}
}
OnTriggerStay2D function is getting called but the condition when a GameObject is completely inside the collider 1 and outside the collider 2 is never true even if the other GameObject satisfies the condition.
Previously when I have attached both collider to the prefab then this condition becomes true when the game object satisfy the condition. But after the change it is not working. I think that I am not accessing the collider of a child properly or there is some other error. Please help me to resolve this problem.
This is the pic of gameobject with two children each having circular collider
and when other game object having circular collider comes in between both the circular collider then I want to perform some action.
this is ultimately what I want to achieve
Edit : the same function is working fine and I am getting what I want when I put the prefab on the scene and then running the game . But it is not working with the same prefab which is instantiated after running the game.
I think you can improve your conditions by using Collider2D.IsTouching like so:
void OnTriggerStay2D(Collider2D other) {
if (collider1.IsTouching(other)) {
print("2");
if (other.IsTouching(collider2)) {
print("3");
if (transform.position.y > 3) {
print("4");
Destroy(other.gameObject);
Destroy(gameObject);
circles.instantiator();
}
}
}
}
Reference: ScriptReference/Collider2D.IsTouching
Check whether this collider is touching the collider or not.
It is important to understand that checking whether colliders are touching or not is performed against the last physics system update; that is the state of touching colliders at that time. If you have just added a new Collider2D or have moved a Collider2D but a physics update has not yet taken place then the colliders will not be shown as touching. This function returns the same collision results as the physics collision or trigger callbacks.
EDIT 1:
You can also combine the previous code with: Physics2D.OverlapCollider
So you can do:
void OnTriggerStay2D(Collider2D other) {
if (collider1.IsTouching(other)) {
Collider2D[] results;
int elements = collider2.OverlapCollider(new ContactFilter2D, results);
if (elements > 0) {
// do some extra work with your result Collider2D array
// here
//
if (transform.position.y > 3) {
print("4");
Destroy(other.gameObject);
Destroy(gameObject);
circles.instantiator();
}
}
}
}

Destroying GameObject and Instantiating a Different GameObject

I have the code for destroying a Cube GameObject when it collides with the Terrain. However, Im not sure how I would then after instantiate a New Sphere GameObject in its place after the cube is destroyed.
This is the current code:
{
void OnCollisionEnter(Collision collision)
{
if (collision.collider.gameObject.tag != "Destroy")
{
Destroy (gameObject);
}
}
}
1) Attach this script to your terrain game object and not the cube.
2) Add a new tag in the editor for cube objects (e.g cube).
3) Create a new sphere prefab instance that you can access through the script containing the OnCollisionEnter() event.
void OnCollisionEnter(Collision collision)
{
if (collision.collider.gameObject.tag == "Cube")
{
//store the transform component of the gameobject to be destroyed.
var transf = collision.gameObject.transform;
//Destroy the collided gameobject
DestroyImmediate(gameObject);
//Instantiate in the position and rotation of the destroyed object.
Instantiate(sphere, transf.position, transf.rotation);
}
}

GameObject colliding with Sprite?

So I have a sprite and it's name is "princess" and when a cube touches my sprite
I want the princess to be destroyed. I added a rigidbody, and a box collider, but for some reason the cube just goes through the princess sprite.
The cube is generated with code so it's name is "Cube" according to the hierachy so I wrote this code
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.name == "Cube")
{
Destroy(this.gameObject);
}
}
I think Destroy(this.gameObject) would destroy the princess, but they're not even colliding.
Any ideas?
Here is what the "game" looks like.
Game
Check if you checked the "is trigger" inside the box colliders.
You should use tag because if you spawn multiple cubes the will have names like "Cube (1)" and so on.
You need to use collider2d
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Cube")
{
Destroy(this.gameObject);
}
}
both of your object needs to have box collider2d and rigidbody2d and not set to "is trigger". example:
Destroy(this.gameObject) is working for own object where
collision.gameObject.setActive("False") is working for collide object
This code is working on my game. where "Player" is my Sprite tag. And make sure 'Is Trigger' is checked for your Cube
void OnTriggerEnter2D(Collider2D other) {
if (other.tag == "Player")
{
Destroy(gameObject);
}
}
OR alternatively in your case below code you could use in Cube script then might be work
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "princess")
{
collision.gameObject.setActive("False")
}
}

GameObject not detecting collision with floor

My GameObject starts out resting on the floor. isKinematic is set to true and a trigger is set to true on the box collider.
When the player touches the collider. It moves the GameObject down below to floor. Then i set the isKinematic to false and the trigger to false. This forces the game object to fall until it hits the floor and stops. My problem is after the game object hits the floor and stops. I can not get the GameObject to recognize it has collided with the floor. I have a Debug.Log statement in OnCollisionEnter2D and OnCollisionStay2D. The Debug.Log does not appear in the console when they touch. Why is this?
The GameObject has a Rigidbody and box collider. The floor has a box collider and Rigidbody as well.
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Player")
{
Debug.Log ("Player is touching the section");
//sectionRigidbody.isKinematic = true;
if (readyToDrop == false)
//moves player
transform.position += newPosition;
readyToDrop = true;
sectionRigidbody.isKinematic = false;
sectionBoxCollider.isTrigger = false;
sectionRigidbody.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
//sectionRigidbody.isKinematic = true;
}
}
void OnCollisionEnter2D(Collider2D col)
{
if (col.gameObject.tag == "Floor")
{
Debug.Log ("section is touching the floor");
}
}
void OnCollisionStay2D(Collider2D col)
{
if (col.gameObject.tag == "Floor")
{
Debug.Log ("section is touching the floor");
}
}
Pay attention when using the collision callback functions with parameter.
OnTriggerEnter2D takes Collider2D as parameter. You got this right.
OnCollisionEnter2D and OnCollisionStay2D takes Collision2D as parameter NOT Collider2D. This is where you failed.
Collider2D and Collision2D sounds so simlar but are not the-same.
They won't be called if you got their parameter wrong.
In the latest version of Unity, error will be thrown in the Editor when you make this mistake. It looks something like this:
Script error: OnCollisionEnter2D This message parameter has to be of
type: The message will be ignored.
and
Script error: OnCollisionStay2D This message parameter has to be of
type: The message will be ignored.
Solution:
Replace
void OnCollisionEnter2D(Collider2D col) and void OnCollisionStay2D(Collider2D col)
with
void OnCollisionEnter2D(Collision2D col) and void OnCollisionStay2D(Collision2D col).
You mentioned that trigger is set to true.
A trigger doesn't register a collision with an incoming Rigidbody. Instead, it sends OnTriggerEnter, OnTriggerExit and OnTriggerStay message when a rigidbody enters or exits the trigger volume.
Source: https://docs.unity3d.com/ScriptReference/Collider-isTrigger.html