I have got some triggers and I want them dependant on tags. This is what I have so far:
void OnTriggerEnter(Collider other)
{
Destroy(gameObject);
}
I want to check to see if the gameobject triggering it has a certain tag
The API is your friend!
Use CompareTag
the example is more or less exactly your code
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Player"))
{
Destroy(other.gameObject);
}
}
Related
Good afternoon, I have a script that should read the hit of the ball in two different objects that I set as
public GameObject LeftHitTrigger;
public GameObject RightHitTrigger;
how to write OnTriggerEnter so that it works with one object and with another
private void OnTriggerEnter(Collider collision)
{
if (LeftHitTrigger.colission, collision.gameObject.tag == "Ball")
{
AiCount++;
Debug.Log(AiCount);
AiCountText.text = AiCount.ToString();
}
if (RightHitTrigger.colission, collision.gameObject.tag == "Ball")
{
AvatarCount++;
Debug.Log(AvatarCount);
AvatarCountText.text = AvatarCount.ToString();
}
}
I tried this but it doesn't work and i get errors after "LeftHitTrigger.colission," and "RightHitTrigger.colission," in brackets.
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
This is the code i am using and i am trying to add a wait inside of the if collision thing anyone know what to look into or a solution? i tried looking into coroutines and using bool values to try and do it that way but i couldnt figure it out any help or suggestions is appreciated!
using UnityEngine;
using System.Collections;
public class SwordDetection : MonoBehaviour
{
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Enemy")
{
collision.rigidbody.constraints = RigidbodyConstraints.None;
}
}
}
You can call a coroutine from within OnCollisionEnter() to create a delay.
Some example code:
private void OnCollisionEnter(Collision collision) {
StartCoroutine(DelayedAction());
// coroutines must be called from StartCoroutine()
}
private IEnumerator DelayedAction() {
yield return new WaitForSeconds(5);
print("I was printed after a delay of 5 seconds!");
}
I'm not sure if this is exactly what you want, but you can use:
OnCollisionStay(Collision collision) { }
I do not know How to call the OncollisionEnter 2D Function in other places of the code.
In THIS CODE I WOULD LIKE TO CALL IT IN THE VOID UPDATE.
I am doing a PLATFORM Jumping game so ot is crucial for me to know how to call the oncollisionenter2D function.
void OnCollisionEnter2D (Collision2D col)
{
if (col.gameObject.name == "Front_Buildings")
{
GetComponent<Animator>().SetBool("isGrounded", true);
}
}
You shouldn't really be explicitly calling OnCollisionEnter2D.
If you need similar behavior, I suggest refactoring to something like this.
void Update() {
if (someReason) {
HandleCollision(someGameObject)
}
}
void HandleCollision(GameObject gameObject) {
if (gameObject.name == "Front_Buildings")
{
GetComponent<Animator>().SetBool("isGrounded", true);
}
}
void OnCollisionEnter2D (Collision2D col)
{
HandleCollision(col.gameObject);
}
I have a trigger zone around a spider terrarium in my game and would like that to trigger the spider's animation when the player walks into the trigger, here is my attempt, but it is returning an error "The name 'col' does not exist in the current context". Thanks in advance!
public class Spider: MonoBehaviour
{
Animator anim;
private void Start()
{
anim = GetComponent<Animator>();
}
private void OnCollisionEnter(Collision collision)
{
if (col.gameObject.tag == "Player")
{
anim.SetTrigger("spider");
}
}
}
The name 'col' does not exist in the current context
This error message already explains the problem: There's no variable called col in your code. The correct name is collision as defined in the method parameter.
private void OnCollisionEnter(Collision collision)
{
if (col.gameObject.tag == "Player")
{
anim.SetTrigger("spider");
}
}
should be
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Player")
{
anim.SetTrigger("spider");
}
}
In your code, the colliding object has been given the name collision, so you need to use that name when checking the variable:
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Player")
{
anim.SetTrigger("spider");
}
}