- Home /
How to call OnTriggerEnter once
I'm creating some sort of score system. There's a GUI Text which will show you your score. Every time you hit a triggered, invisible object, your score will raise by one. However, if I use the function "OnTriggerEnter" it gets called at least 14 times. It gets called in the time you are IN the object. I believe OnTriggerStay should function like that.
Anyway, here is my code:
public static int pScore = 0;
public GUIText ScoreText;
void OnTriggerEnter2D(Collider2D collider)
{
if (collider.gameObject)
{
pScore++;
ScoreText.text = "" + pScore;
print("Test");
}
}
Thanks
Answer by GenuiTix · Feb 07, 2014 at 09:23 AM
Most likely you should be more precise while checking what cause collision.
In
if (collider.gameObject)
check also for something unique for you main object, as name or tag.
Forgot to edit the code. I did that right after I posted this question. It's now looking for a name.
Are you sure that your GameObject (which triggers the collision) is not the composition of many colliders?
If it is, every collider included will trigger on enter function. You can try execute same code with a very simple GameObject which has one collider.
You can add some Debug.Log(collider.name) to see is your object only one that trigger the collision
Also you can add OnTriggerExit2D to see are there any other unexpected transitions (e.g. Input $$anonymous$$ey Pressed event is invoked twice per key pressing, $$anonymous$$ey pressed down is invoked once per key pressing)
I'm specifically looking for a name. It doesn't react on every colliding object, it reacts on the right object. Here is the code: public static int pScore = 0; public GUIText ScoreText;
void OnTriggerEnter2D(Collider2D collider)
{
if (name == "DetectionLine")
{
if (pTouched == 0)
{
pTouched = 1;
print("Test");
// Do something
}
}
}
void OnTriggerExit2D(Collider2D collider)
{
if (pTouched == 1)
{
pTouched = 0;
}
}
Test will only get printed once, as it should.
p/s Don't worry about the commenting. I comment everything, but in my native language. I removed it in this example
Checking the name should be more like
if (collider.name == "DetectionLine")
Currently you are comparing the name of GameObject to which your script is attached.
Yes, however, collider.name returns the name of the object colliding to DetectionLine.
Answer by davidc · Feb 06, 2014 at 11:05 AM
What if you tried,
public static int pScore = 0;
public GUIText ScoreText;
void OnTriggerEnter2D(Collider2D collider)
{
if (collider.gameObject)
{
pScore++;
gameObject.collider2D.enabled = false;
ScoreText.text = "" + pScore;
print("Test");
gameObject.collider2D.enabled = true;
}
}
This may work but might not, sorry if it doesnt work. I had a simliar problem where my player would "jump" and "doublejump" at the same time.
I hope it works for you.
Unfortunately that didn't work. I "by-passed" it by adding a variable to check if the object is inside the 2d collider, once the object exits the collider, the variable will reset. But I'm not sure if this is the proper way to "fix" this, as OnTriggerEnter should only be called once.
Answer by kannan21 · Feb 06, 2014 at 11:53 AM
FYI OnTriggerEnter is called only once by default, when ever some thing enter the trigger. So you don't have to worry about calling it only once. Only OnTriggerStay is called repetitively untill the object leaves the trigger.
Answer by hendryshaikh2004 · Oct 14, 2020 at 02:18 PM
You can do it like this use bool to check whether it has finished or not if it helps give it alike
[SerializeField] int ammoCount = 5;
[SerializeField] AmmoType ammoType;
bool picked = false;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player" && !picked) // I'm using bool to check whether it has trigger
{
print("Gained Ammo");
FindObjectOfType<Ammo>().IncreaseAmmo(ammoType, ammoCount);
picked = true;
Destroy(gameObject);
}
}
`
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
On Trigger Enter, Collide with object, specific collision 1 Answer
Animation with frozen player ?? 0 Answers
Problem with acing problems with : OnTriggerEnter 0 Answers
Scene Change Collision 2 Answers