- Home /
On trigger active object, else not c#
Hey guys, sorry to bother. I have two gameobject in my game, one with a trigger, one with a render. I'd like the trigger to activate the render when something is touching it, but to deactivate it when it's not. I thought I had it nailed with this using UnityEngine; using System.Collections;
public class warning1 : MonoBehaviour {
public GameObject myrender;
void OnTriggerStay () {
Debug.Log ("Warning!");
myrender.SetActive (true);
}
void OnTriggerExit () {
myrender.SetActive (false);
}
}
But it doesn't work. When something enter the collider it does turn on the render object, but it doesn't turn it off when the collider is empty again...
EDIT: I'm trying to set up a warning system against my enemy with this in fact. So I put on my enemies an empty object with a collider and set it so the trigger can only be touched by those. That way, when my enemies reach the trigger, the render should go on and alert my players, and once he destroy them, the render should go off. The log tell me my enemies entering the trigger is detected, but when the enemies are destroyed (and their triggering collider with them) the render don't go off. However if I move the trigger on top of my enemies, then away again, the render does go on and off as I want. It seems as if the trigger doesn't detect my enenmies destruction as a way of leaving the trigger.
maybe you need a flag (boolean variable) to check the state and check that flag in Update function to active or deactive something !
How you can enter in something if it's not even active? You would like to disable rendering by "renderer.enabled = false", but if object have physics it will interact while invisible anyway.
It looks like your code should work. Add a Debug.Log to the Exit to show when that's being called. I can only think that Stay is somehow still triggering on the same frame that Exit fires. You could also try OnTriggerEnter ins$$anonymous$$d of OnTriggerStay as that only fires when it first enters.
The object with trigger is always active. It's the object with render that I want to turn on and off. I know I could just turn the render on and off, but it seems easier to turn the whole thing off. Less referencing code to do.
Answer by Anxo · Jan 02, 2015 at 07:52 PM
You might not be hitting what you think you are. Try this.
void OnTriggerEnter(Collider other) {
Debug.Log(other.transform.gameObject.name);
}
void OnTriggerExit(Collision collisionInfo) {
Debug.Log(collisionInfo.transform.name);
}
WUCC
See if the results are as expected.
I tried, and yes, it is the right item that collide. I'm trying to set up a warning system against my enemy with this in fact. So I put on my enemies an empty object with a collider and set it so the trigger can only be touched by those. The log tell me that's what happen upon entry, but when the enemies are destroyed (and their triggering collider with them) the render don't go off. However if I move the trigger on top of my enemies, then away again, the render does go on and off as I want... I really don't get what I do wrong...
Ah so that is useful information, so it works if you move out of the trigger but exit is not called if the other collider is destroyed.
In that case you can set up a method that handles the OnTriggerExit outside of the method itself so you can handle it when the enemies are destroyed.
void OnTriggerEnter(){
myRenderer.SetActive(true);
}
void OnTriggerExit(Collision collisionInfo){
HandleTriggerExit(collisionInfo.transform.gameObject);
}
void HandleTriggerExit(GameObject enemy){
myRenderer.SetActive(false);
}
void Some_$$anonymous$$ethod_That_Destroys_Enemies(){
HandleTriggerExit(someEnemy); // Then you can call this from anywhere to turn off the renderer before you destroy the enemy.
Destroy (someEnemy);
}
Warning Uncompiled Code
I'm sorry but I'm not that well versed in Unity yet, so I'm not quite sure how to use the code you gave me... I have a script to kill my enemies, with a void $$anonymous$$ill method, I tried to place that ins$$anonymous$$d of the 'Some-method' you wrote, but it doesn't work. I'm probably doing something wrong, I know...
Answer by ChrPunx · Jan 02, 2015 at 08:41 PM
void OnTriggerEnter(Collider Trigger){
if(!myRender.activeInHierarchy && trigger.collider.gameObject.tag == "targetTag"){ // if you want to activated only for a specific object with a specific tag
myRender.SetActive(true);
Debug.Log("Render activated");
}
}
void OnTriggerExit(Collider Trigger){
if(myRender.activeInHierarchy && trigger.collider.gameObject.tag == "targetTag"){ // if you want to deactivated only for a specific object with a specific tag
myRender.SetActive(false);
Debug.Log("Render deactivated");
}
}
Your code allowed me to realize something. I'm trying to set up a warning system against my enemy with this in fact. So I put on my enemies an empty object with a collider and set it so the trigger can only be touched by those. The log tell me that's what happen upon entry, but when the enemies are destroyed (and their triggering collider with them) the render don't go off. However if I move the trigger on top of my enemies, then away again, the render does go on and off as I want. It seems as if the trigger doesn't detect my enenmies destruction as a way of leaving the trigger.
does you enemies have a Health script on them? if so, add something like...
OnTriggerStay(...)
if(col.collider.gameObject.getComponent<Health>().dead == true){
// what to do
}
(...)
Yes they do,but I'm not that well versed in Unity yet, so I'm not quite sure how to use the code you gave me... Unity say col. is something unknown to it...
place a bool on your Health script, and this on your Update method
if(health <= 0){ dead = true; }
/// on the trigger script
void OnTriggerStay(Collider triger){
if(col.collider.gameObject.getComponent< your health script>().dead == true){
myRenderer.SetActive(false);
}
}
$$anonymous$$y health script is called Life, in it I put a bool, like you said, called dead. and I added the line you gave me in Update.
Then I copied and pasted the trigger code into my trigger script, but since it kept saying 'col.' doesn't exist, I tried fiddling it, see if I could make it work. Tried this
void OnTriggerStay(Collider other){
Life l = other.gameObject.GetComponent<Life>();
if (l.currentLifePoints <= 0){
myrender.SetActive(false);
it tells me the instance isn't declared. Same for this
void OnTriggerStay(Collider other){
Life l = other.gameObject.GetComponent<Life>();
if(l.dead == true){
myrender.SetActive(false);
And this one same thing
void OnTriggerStay(Collider other){
if(other.gameObject.GetComponent<Life>().dead == true){
myrender.SetActive(false);
Your answer
