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
Related
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
}
}
}
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);
}
}
As I was trying to make a game in which player kick the ball when the player collides with ball and as I press Q but it is not working and not showing any error
public Rigidbody rg;
void OnCollisionEnter(Collision col)
{
if(col.gameObject.name=="ball")
{
if(Input.GetKeyDown(KeyCode.Q))
{
rg.AddForce(100,0,0);
}
}
}
Potentially try to add another sphere collider to the ball, and make that a trigger
then it is a matter of changing :
public Rigidbody rg;
void OnCollisionEnter(Collision col)
{
if(col.gameObject.name=="ball")
{
if(Input.GetKeyDown(KeyCode.Q))
{
rg.AddForce(100,0,0);
}
}
}
To :
public Rigidbody rg;
void OnTriggerEnter(Collision col)
{
if(col.gameObject.name=="ball")
{
if(Input.GetKeyDown(KeyCode.Q))
{
rg.AddForce(100,0,0);
}
}
}
The idea is that collision in unity is finicky, so your best bet is just to have a larger trigger to detect if the player is within range of the ball
Side NOTE :
I'm guessing that this is on your player, so maybe you could try this as well :
keep in mind that the "if" statement is just in case the ball object has different
name extensions, or maybe "ball (clone)" etc.
Rigidbody rb;
void Start(){
rb = gameObject.GetComponent<Rigidbody>();
}
void OnTriggerEnter(Collision col){
if(col.gameObject.name.ToString().Contains("ball") &&
Input.GetkeyDown(KeyCode.Q))
{
//here you can add force to the player, or to the ball
// for the player :
rb.AddForce(100,0,0);
// for the ball
col.GetComponent<Rigidbody>().AddForce(100,0,0);
}
}
This should give you a step in a different direction, Hopefully, this helps!
Cheers!
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);
}
}
When my Player (GameObject) meets Lava, they should respawn in a specific scene.
This is the code I have assigned to the Player:
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Lava")
{
GameObject.Find("Controller").GetComponent<Controller>().Respawn();
}
}
Controller is a GameObject, that I don't want to Destroy by Changing level, so this is the code for my Controller GameObject:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Controller : MonoBehaviour
{
private static bool created = false;
public static Controller instance;
GameObject Player;
Vector3 respawnPoint;
void Awake()
{
if (instance = null)
{
instance = this;
}
else
{
Destroy(this.gameObject);
return;
}
if (!created)
{
DontDestroyOnLoad(this.gameObject);
created = true;
Player = GameObject.Find("Player");
respawnPoint = GameObject.Find("RespawnPoint").transform.position;
}
}
public void Respawn()
{
SceneManager.LoadScene(0);
Player.transform.position = respawnPoint;
}
}
RespawnPoint is just an invisible Cube GameObject, where I want the player to respawn.
Let's say the Game Starts with Scene "0" (this is where the RespawnPoint is, too.)
Then the Player goes to Scene "1" and dies (meets Lava). Then I want the Game to change back to Scene "0" and teleport the Player to the RespawnPoint.
The Scene-Change works good, but the player always starts at the same position, where he starts the first time and he's not teleported to the RespawnPoint.
What am I doing wrong?!
First of all you lack the "==" in the first 'if' from the Awake: if (instance == null
The code is fine or it does seem so to me, but the RespawnPoint should be in the Scene your Player meets the lava not in the Scene you are loading. If not the starting position of the player will always be (0,0,0).
I would recommend coming to this in a completely different way. I would make a public Transform[] Spawnpoints. Since the transform is public you can assign different objects to it. Make an empty game object and position it where you want to spawn. Then use
Void OnTriggerEnter(collider2D, other){
if(other.gameObject.tag == lava) {
transform.position = spawnpoints[0].position;
}
}
In the inspector set the Transform to be a size of 1 and set the respawn GameObject as the one transform.
Thanks to your answers, they helped me solving this problem.
I added a "DontDestroyOnLoad" to the RespawnPoint and than i changed the Controller Code to this:
{
private static bool created = false;
public static Controller instance;
void Awake()
{
if (instance == null)
{
instance = this;
}
else
{
Destroy(this.gameObject);
return;
}
if (!created)
{
DontDestroyOnLoad(this.gameObject);
created = true;
}
}
public void Respawn()
{
SceneManager.LoadScene(0);
GameObject.Find("Player").transform.position = GameObject.Find("RespawnPoint").transform.position;
}
}
now the player gets teleported to the correct RespawnPoint. Thanks for your help!