Nothing gets destroyed? - c#

Nothing gets destroyed? I have one enemy which is a sphere with a collider and a rigidbody and a cube with the same. I've tried enabling triggers on either one but gravity goes wack and nothing gets destroyed! Please help!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyOnCollision : MonoBehaviour
{
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.CompareTag("Player"))
{
Destroy(collision.gameObject);
}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Enemy"))
{
Destroy(other.gameObject);
}
}
}

You need to add your Script to the Player and to the Enemy GameObjects instead of putting them into one separate Script.
Because OnTriggerEnter and OnCollisionEnter get called on the Object that is colliding or entering the Trigger
You can't just make a universal script that handles all Collision, because that Script or Object isn't colliding with anything. Because of that the functions never gets called and therefore nothing gets destroyed.
Changed Player Script (Add):
// The Enemey needs to have IsTrigger enabled
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Enemy"))
{
Destroy(other.gameObject);
}
}
Changed Enemy Script (Add):
// Player needs to have IsTrigger disabled
private void OnCollisionEnter(Collision collision)
{
if (collision.collider.CompareTag("Player"))
{
Destroy(collision.gameObject);
}
}

Related

In Unity I can't figure out what to put as a parameter

private void OnCollisionEnter(Collision collision) {}
private void Update()
{
OnCollisionEnter("Here"); <----
}
OnCollisionEnter is an event callback and is not supposed to be called as you have done.
Unity automatically calls it when the object (which is a RigidBody) collides with another object with a collider. For example, consider a Player object colliding with the Enemy object
public class Player : MonoBehaviour
{
public GameObject player;
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "Enemy")
{
//Destroy Player
}
}
}

Display text in unity onTrigger

I want to display a text in unity by a trigger, but it isnt working, this is my code. I have a collider with "Is Trigger" activated, the tags are ok, i dont know what its happening...
public class LaptopTriggerCollider : MonoBehaviour
{
public GameObject UiObject;
public GameObject cube;
void Start()
{
UiObject.SetActive(false);
}
void OnTriggerEnter(Collider other)
{
if (other.tag.Equals("player"))
{
UiObject.SetActive(true);
}
}
void Update()
{
}
void OnTriggerExit(Collider other)
{
UiObject.SetActive(false);
}
}
Firstly try to debug by putting a log statement in OnTriggerEnter function
void OnTriggerEnter(Collider other)
{
Debug.Log("Is this even being triggered?");
if (other.tag.Equals("player"))
{
UiObject.SetActive(true);
}
}
I presume it won't print in console, reason could be that you are missing a rigidbody in the collision.
A rigidbody is a must for collision or trigger events to be generated in Unity.
Instead tag.Equals use CompareTag
if(other.CompareTag("player"))
{
UiObject.SetActive(true);
}
CompareTag

OnCollisionEnter2D not called

I am making a script on an elevator that is supposed to teleport the player when they are next to it and press e
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class interactible : MonoBehaviour
{
public Transform player;
public Transform pos;
private bool collide;
void Start()
{
collide = false;
}
void OnCollisionEnter2D(Collision2D collider)
{
collide = true;
}
void OnCollisionExit2D(Collision2D collider)
{
collide = false;
}
void Update()
{
if (Input.GetKeyDown("e") && collide)
{
player.position = pos.position;
}
}
}
I have defined both player and pos and have made a 2d box collider for the elevator and set "is trigger" to true.
What am I doing wrong?
Your issue is that 2 triggers can't collide, only triggers and colliders can work with each other.
UPDATE: The main problem turned out to be that when you're dealing with triggers, you need to use OnTriggerEnter2D instead of OnCollisionEnter2D
Input.GetKeyDown accept enum so it should be
if(Input.GetKeyDown(KeyCode.E) && collide)

Health system script not running in Unity; no errors or Debug.Log statements popping up

For my health system in a Unity game, I have a script that is accountable for my in-game "enemy" hit-points. The game runs just fine, but the script doesn't seem to be doing anything. I'm not getting any error messages, but the fact that it's not working and Debug.Log statements aren't popping up in console seem to be that functions aren't being called properly or that something else is awry. Here is my script:
using System.Diagnostics;
using UnityEngine;
public class Health : MonoBehaviour {
private float hitPoints = 5;
// Health popup
void announceUp()
{
UnityEngine.Debug.Log("If this message shows in Debug.Log, the script should be working.");
}
// Update is called once per frame
void Update()
{
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Bullet")
{
UnityEngine.Debug.Log("The enemy has been hit!");
hitPoints = hitPoints - 1f;
if (hitPoints == 0f)
{
UnityEngine.Debug.Log("The enemy has been eliminated!");
Destroy(gameObject);
}
}
}
}
}
I've poked around the internet to see what's wrong, but I couldn't find anything. Could someone inform me on what could be wrong with my programming?
Your scrpt is currently not working, because you are defining the OnTriggerEnter() Method inside the Update() Method. When you do that you are defining a local function and Unity then can't call that function when actual collision happens. So your OnTriggerEnter() Function never get's called.
Example:
using System.Diagnostics;
using UnityEngine;
public class Health : MonoBehaviour
{
private float hitPoints = 5;
// Health popup
void announceUp() {
UnityEngine.Debug.Log("If this message shows in Debug.Log,
the script should be working.");
}
// Update is called once per frame
void Update() {}
void OnTriggerEnter(Collider other) {
if (other.gameObject.tag == "Bullet") {
UnityEngine.Debug.Log("The enemy has been hit!");
hitPoints = hitPoints - 1f;
if (hitPoints == 0f) {
UnityEngine.Debug.Log("The enemy has been eliminated!");
Destroy(gameObject);
}
}
}
}

ThirdPersonController collision not calling OnTriggerEnter event

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour {
public Transform target;
// Update is called once per frame
void Update ()
{
}
private void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "Test")
{
this.transform.position = target.position;
}
}
}
I have a ThirdPersonController and i want it to collide with a cube or cylinder.
The script is attached to the ThirdPersonController.
I tried to add either to the cylinder or the cube Rigidbody turned on/off the Use Gravity and the Is Kinematic but nothing. It's not getting to the event.
ThirdPersonController uses CharacterController and OnControllerColliderHit is used for that not OnTriggerEnter.
Note that you must move it with the Move function not directly by its transform in order for OnControllerColliderHit to be called.
void OnControllerColliderHit(ControllerColliderHit hit)
{
}
Every thing is correct but the thing wrong here is you are changing the position of the object which is colliding with a second object , but the thing id the thing is the collider is already there.....
As it's alternative try to print any statement when collision happens like this
private void OnTriggerEnter(Collider other)
{
if (other.tag==your_tag)
{
print("message");
}
}

Categories

Resources