- Home /
Play method only ONCE when detected by the update function
It seems like there should be a pretty simple solution to this problem I'm having but I can not figure it out no matter how hard I try. I'm just trying to make it so that once the update method gets to the "applykillforce();" method (in line 41), it only runs it once, and never gets played again. Right now it runs it over and over again, which is not what I want (because it makes the enemy spaz out).
If anyone has a solution, it would be greatly appreciated, as I have been stuck on this for awhile.
BTW I am new to coding so if you guys could be specific and try to keep it simple for me that would also be great.
Answer by browne11 · May 25, 2018 at 01:42 AM
If you just want to run it once, perhaps putting this in your start function would be better served.
void Start(){
if(Peeker.GetCompenent<PeekerDie>() != null){
if(Peeker.GetCompenent<PeekerDie>().enable == true){
applykillforce();
}
}
}
or you could setup a bool to trigger it once while keeping it in the update sequence.
bool runOnce;
void Start(){
runOnce = false;
}
void Update(){
if(runOnce == false){
if(Peeker.GetCompenent<PeekerDie>() != null){
if(Peeker.GetCompenent<PeekerDie>().enable == true){
applykillforce();
runOnce = true;
}
}
}
}
Also, another tip. Try to name your variables like this: applykillforce change to Apply$$anonymous$$illForce
This will make it easier to read.
Note when declaring variables keep the first letter lowercase
ex: runOnce.
It will appear in the unity editor like Run Once ins$$anonymous$$d of runonce. :)
Put a debug statement in your code to see if it's firing multiple times or not. $$anonymous$$y guess is the amount of force you're throwing at it with the 16000 number is making it look really odd.
applykillforce();
Debug.Log("$$anonymous$$ill Force");
runOnce = true;
It will show up in the console, if you see it growing in numbers then it's firing multiple times.
Answer by JusSumGuy · May 25, 2018 at 05:25 AM
Setting up a bool is a good way to do what you want. But if you came across a situation that required you to call the function twice, three , 4 .... etc. times. I think this is a better way to approach it. And btw your right there is a simple way. I don't know why people are bringing Coroutines into this. All you have to do is make a int variable count and set it equal to 0 in the beginning. Then when you call your function check if count is equal to 0 if it is then call the function and add one to count. Now the function wont be called again because count doesnt equal 0 anymore. Here's the code hope it helps!
int count = 0;
private void Update
{
if( count == 0 && Peeker.GetComponent<PeekerDie>().enable == true){
count++; // now count is 1 , so next time the if statement will return false and your code wont run more than once.
applykillforce();
}
}
void applykillforce()
{
GetComponentInChildren<Rigidbody>().AddForce(-transform.forward * 16000);
}
I think I know what the problem is. Your using GetComponentInChildren, do you have more than one child on that object? if so then it will run the function on all of them. You need to use GetComponentInChild and be specific as to which child you want to Add the force to.
Your answer
Follow this Question
Related Questions
Play method only ONCE when detected by the update function 3 Answers
Show message when score achieved 1 Answer
C# void names 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers