- 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 Lost_Syndicate · May 24, 2018 at 02:12 AM
Ok, first off your using a Update
method, which it's going to be called every frame if Peeker is enabled. Second you should use a tick method;
void Test()
{
bool ticked;
if(ticked && Peeker.GetComponent<PeekerDie().enabled)
{
ticked = false
// Run code here
}
else if(!ticked && !Peeker.GetComponent<PeekerDie().enabled)
{
ticked = true
}
}
If this need revisions, or this doesnt work, just comment.
Note: This can be runned multiple times, if you dont want that remove the else if statement, and set ticked to true. Also, this can be used in an update method, but the ticked will keep switching to true and false, if peeker is also enabled or disabled, you can run void Test on Update.
This really confuses me. Is the bool "ticked" supposed to be different from the bool "tick?"
And also, what is "void Test()?"
I udpated it, also if you dont know what a function is, try to learn it.
I understand what a function is.
I just integrated your code but it didn't seem to work. There was no knockback in the rigidbody.
Answer by winterfluxstudio · May 24, 2018 at 02:48 AM
// has the method been run already?
public bool hasAppliedKillForce;
void Update()
{
if (Peeker.GetComponent<PeekerDie>().enabled == true && hasAppliedKillForce == false)
{
// use a coroutine
StartCoroutine(applyKillForce());
}
}
IEnumerator applyKillForce()
{
// do something
// ensure coroutine can not be triggered again
hasAppliedKillForce = true;
yield return null;
}
https://docs.unity3d.com/Manual/Coroutines.html https://docs.unity3d.com/ScriptReference/Coroutine.html
This isn't working for me. It gives me a red underline under the IEnumerator "apply$$anonymous$$illForce"
sorry, forgot something. will update. Forgot to add
yield return null;
Coroutines can return stuff. ex. wait 5 seconds then do something. but if you dont return anything you have to declare null
yield return new WaitForSeconds(5);
// do something
This is what I got so far. I think I'm missing something but I don't know what it is.
Answer by JusSumGuy · May 24, 2018 at 11:13 AM
killForceCount++;int killForceCount = 0;
Update() { If(killForceCount == 0 && your condintion ) {
} } Just add a counter variable and add one to it each time you call the function. If you only want to call it 3 times for example then you would say if count is less than 3 call the function. Otherwise don't. :) sorry for the sloppy code writing on my phone.ApplyKillForce();
You don't need a coroutine either you can just make it a normal method. Hope this helps!
This is what I have right now, but so far nothing happened.
Am I maybe putting the method "applykillforce" in the wrong place?
Initialize your variable killForceCount.
write this before your update method and I don't see why it shouldnt work.
int killForceCount = 0;
Your answer
Follow this Question
Related Questions
Play method only ONCE when detected by the update function 2 Answers
Show message when score achieved 1 Answer
C# void names 1 Answer
Update within an if statement not working? 3 Answers
Multiple Cars not working 1 Answer