Hello, simple question. Not too sure if this is a bug or not, anyone else notice that OnTriggerEnter and OnTriggerExit only work once and refuse to send messages after the first time? I’m adding enemy spawn cubes with script that spawns enemies when Lerpz enters the cube sphere collider and deletes them when he exits. Problem is, the colliders only trigger once. I’m adding this to the 3D platform tutorial btw. Anyone else have this happen to them?
Hi Eric5h5, wonder why it’s working only once for me though. You can see the script below, it has the additions you helped me with. I have the spawn cube move randomly on it’s X position thinking it would reset the OnTrigger but it doesn’t. Lerpz runs into the cubes sphere collider and enemies spawn and when Lerpz leaves the sphere collider, the enemies get destroyed. When I try to move the player back into the collider, nothing happens. Would keeping a InCollider = false; line be good for this?
Thanks again.
var copper : GameObject;
var XPositionValue = 0.0;
var YPositionValue = 26.0;
var ZPositionValue = 0.0;
var XRotationValue = 0.0;
var YRotationValue = 0.0;
var ZRotationValue = 0.0;
var enemyCountOne = 0;
function UpdateStats1 ()
{
if (enemyCountOne <10)
{
newEnemy = Instantiate(copper.gameObject, Vector3(Random.Range(-64,-202), 26, (Random.Range(92,-33))), Quaternion.Euler(0,(Random.Range(360,-360)),0));
newEnemy.name += "enemyGen01";
var groupParent : GameObject = GameObject.Find("EnemyClone01");
newEnemy.transform.parent = groupParent.transform;
enemyCountOne++;
}
}
function MoveCube ()
{
var YPosCube = 25;
var ZPosCube = 26;
transform.position = Vector3(Random.Range(-132,-134),YPosCube,ZPosCube);
}
function OnTriggerEnter ()
{
InvokeRepeating("UpdateStats1", 0, 1);
}
function OnTriggerExit ()
{
var enemyExit : GameObject = GameObject.Find("EnemyClone01");
for (child in enemyExit.transform)
{
Destroy (child.gameObject);
}
}
function Start ()
{
InvokeRepeating("MoveCube", 0, 1);
}
One thing I see is that you have InvokeRepeating happening in OnTriggerEnter, but it’s never cancelled, so that every time an OnTriggerEnter event occurs, you have an additional instance of the UpdateStats1 function being invoked repeatedly.
Basically the problem is that OnCollisionEnter is only called when the object first intersects, after which all future collisions can be retrieved from the OnCollisionStay function until the object is no longer being intersected. I believe the last intersecting object triggers the OnCollisionExit function but don’t quote me on that.
Hi Eric5h5, great point, never noticed the bug I made
The new code works perfectly.
function OnTriggerEnter ()
{
if (enemyCountOne < 10)
{
newEnemy = Instantiate(copper.gameObject, Vector3(Random.Range(-64,-202), 26, (Random.Range(92,-33))), Quaternion.Euler(0,(Random.Range(360,-360)),0));
newEnemy.name += "enemyGen01";
var groupParent : GameObject = GameObject.Find("EnemyClone01");
newEnemy.transform.parent = groupParent.transform;
enemyCountOne++;
}
}
function OnTriggerExit ()
{
var enemyExit : GameObject = GameObject.Find("EnemyClone01");
for (child in enemyExit.transform)
{
Destroy (child.gameObject);
enemyCountOne = 0;
}
}
function Start ()
{
}
Hi The IT, the code above works perfectly for me now. You can thank Eric5h5 as well, he was patient with me and my newbie ways but I’m learning and hope that others can learn from this.
This seems like it should be insanely easy, but it isn’t presenting itself that way so far.
I’m trying to use the “OnTrigger…” functions to read when there’s a collider in a trigger, then read when it’s not. If someone could verify my code here and let me know what I’m doing wrong I would be eternally grateful!
here’s my post from another thread, but edited to reflect the changes per someone else’s attempt at helping me (to no avail!):
does OnTriggerBlaBla reset itself once the event has been triggered and ended? I see no sign of this based on the code I’ve provided in my previous post.
remember there is also OnCollisionStay and OnTriggerStay that are called once per frame, when you want to constantly checking if something is hitting something or if its inside of something, the other 2 are only when you start the event and when it ends, if you are just activating a toggle those 2 are enough one for enabling it and the other for disabling it, but for continuos checking thats what stay is for
Thanks for the reply! Unfortunately it’s not performing as I’d expect. I posted my code in a previous post so you can see how I’m using it. It seems like it SHOULD work but it doesn’t.
I’m manually pushing a cube (with kinematic rigidbody attached) into a sphere (with trigger collider attached). I get a “true” feedback from Debug.Log but it won’t change back to “false” when I exit the sphere. (yes, both are tagged as “Player”.)
Is the object leaving the trigger by being pushed or does it “teleport” out by having its position changed from the script? I’m not sure how well a trigger tracks objects that suddenly disappear when they are completely inside.
What I’ve tested it with actually manually manipulating the object in and out of the sphere while it’s playing. It registers “true” when it hits, but not “false” when I drag it out of it again.
Mind you, this is a test scene because I’ve had this same problem in my game where the object is controlled into the collider (returning “true”) then controlled out of it (without returning to “false”). Grr.
Have you tried stripping it down to the bare minimum? In your test scene, you shouldn’t need the tag check. Try it without a bool as well, in case there is some sort of variable name collision or scope issue (I always use the this keyword, but it shouldn’t matter.) Basically. Start simple and build up to the issue.
Try just this:
using UnityEngine;
public class CheckPlayerProximity : MonoBehaviour
{
void OnTriggerEnter(Collider other)
{
Debug.Log("ENTER");
}
void OnTriggerStay(Collider other)
{
Debug.Log("STAY");
}
void OnTriggerExit(Collider other)
{
Debug.Log("EXIT");
}
}
I’m sure you are doing this part already, but as a head check. I just did this with the script above and it works fine…
Start with a clean scene
make a primitive SPHERE and a primitive CUBE.
Set the SPHERE isTrigger to True.
Add a rigidbody to the CUBE, turn off gravity and turn on isKinematic.
Add the script to the SPHERE
Press play and it should work.
By the way, I tried this with a mesh collider and it doesn’t work the same way. It seems to trigger onExit as soon as ‘other’ is no longer touching the surface. In side or out doesn’t seem to matter. I just posted this on the support board to see if this is right.
Hey there… Thanks for trying to help but unfortunately this still doesn’t do what I want it to. Each function appears to only happen once and is “expired” after they execute, it seems. So if I move the cube back and forth inside and outside the sphere over and over, it only reports “STAY” and “EXIT” once, then it’s done with the whole thing, like a “use once throw away” kind of deal. Is that what you’re expecting to happen?
And yes, I’ve tried to strip it down to a bare minimum. Actually that was the code I provided earlier… it was the boiled-down version of my “piece of the puzzle”, as it were. The game I’m doing has much more going on, but this bit of logic is all I’m trying to work out at the moment before I plug it into the main game.
Thanks again! Please can you or anyone verify that the OnTrigger series works like this?
When ANY object enters it fires off OnTriggerEnter
When ANY object leaves it fires off OnTriggerExit
As long as ANY object stays inside, OnTriggerStay runs once per frame
All of these run per object and do not wait. So if an object enters then:
OnTriggerEnter (for first object)
then another one:
OnTriggerStay (for first object)
OnTriggerEnter (for second object)
then they are both inside
OnTriggerStay (for first object)
OnTriggerStay (for second object)
then the second one exits:
OnTriggerStay (for first object)
OnTriggerExit (for second object)
This might be a long shot, but have you checked to see if “Collapse” is OFF in the Unity Console? I am embarrassed to admit this “feature” ruined my night a few days ago. I thought something in my new GUI code was corrupting the GUI calls and no longer reporting mouse clicks. It turns out Collapse was ON. Funny now, not so funny a few nights ago!
How about the following: It is actually all working correctly but you have plain simply forgotten to disable “collapse” in the editor console :twisted:
:lol: It will be funny in a few days, trust me. :twisted:
Does anyone have a use fro that feature?
It seems like it would be good if the latest message always showed up last. it just looks static. I mean, when I was logging GUI events, the order of the messages displayed never changed. I would expect repaint, etc, to constantly jump to the bottom of the list as they occur. At least I would have noticed something was up!
All these years later in Unity 5.5 and this Collapse feature is still tricking people! If the latest debug.log were at the top each time it wouldn’t be a problem. Thanks rsx!