Counting OnEnterStay or other methods
Hi everyone. I am in the process of coding a game for a college class but I am running into some logical problems with OnTriggerStay and C# in general since I studied mainly Java in High school. Is there a way to count the number of time OnTriggerStay is called and store it in a variable? It currently displays the number of frames its called in the console. I also wondered if its possible to use FixedUpdate to call OnTriggerStay but I dont know much. If anyone could help that would be great. Sorry for the wall of text.
Answer by M-Hanssen · May 11, 2016 at 08:56 AM
Please read the manual for "OnTriggerStay" http://docs.unity3d.com/ScriptReference/Collider.OnTriggerStay.html
You will see that this callback is called by the physics timer (with good reason of course), so basically it is already called from FixedUpdate!
To store the number of times the OnTriggerStay is called is very easy and with your Java background trivial to give you an example. But here it is:
public int OnTriggerStayCounter;
protected void OnTriggerStay(Collider other)
{
OnTriggerStayCounter++;
}
Could you explain what you are trying to do exactly?
I am trying to count the number of times OnTriggerStay is called so when it is called x amount of times I will call the re spawn function on my character. If there is an easier or more efficient way to do this then feel free to give me suggestions. $$anonymous$$y goal is to use an if statement to do something like this (in pseudo). If OntriggerStayCounter equals 30 then Character1.Respawn() or something like that.
Something like this?
public int OnTriggerStayCounter;
public int $$anonymous$$axTimesBeforeRespawn = 30;
protected void OnTriggerStay(Collider other)
{
OnTriggerStayCounter++;
if (OnTriggerStayCounter >= $$anonymous$$axTimesBeforeRespawn) Character1.Respawn();
}
Exactly like that. Thanks so much for the help! :)