- Home /
How can the bullet buff the other for a few second?
i know that a function can set the time for destroy the gameobject after a few second--(Destroy( Prefab ,2.0f);) Is there a way to control the other script variable for a few second??
My method is add component (get and set the variable , set timer ) while the time is up return the variable to the gameobject and removecomponent...
Answer by tormentoarmagedoom · Sep 26, 2018 at 06:45 PM
Good day.
Yes, you have 2 options.
The first is use Invoke , wich allows you to runa method in the same script after X seconds. (this can solve your problem, but is not the best solution for future problems like this)
You need to learn aboout Corutines. Its SUPER EASY!
You only need to create a new fuction, but dont do it "void" type; do it "IEnumerator ", like this:
IEnumerator MyFunction()
{
Do something A
Do something B
yield return new WaitForSeconds(3);
Do something C
}
When this funciton is called, it will do A, B, will wait 3 seconds, and then will do C.
Of course, if you do this:
IEnumerator MyFunction()
{
yield return new WaitForSeconds(3);
Do A
Do B
Do C
}
It will wait 3 seconds, and then do A, then B, then C.
This was a Corutine, not a "regular function", so to call it, you need to:
StartCoroutine( MyFunction() );
So for your problem, you can do something like this (if I understood what you need) :
StartCorutine( MyFunction(4.5f) );
IEnumerator MyFunction(float EffectTime)
{
Apply effects to Object;
yield return new WaitForSeconds(EffectTime);
Remove effects from Object;
}
So the object will have that effects during 4.5 seconds
You can use multiple yield comands inside the method (at least needs 1), to control timing of execution. You can call again the Corutine at the end of the corutine to loop its effect, you cna do sooo many things...
Thats all! Simple, Easy and effective for your problem!
Once you are ready, go leran more about corutines, because allow you to easy repeat some function, or divide parts of a funtion in different frames, etc...
Good luck!
Bye!
Hi there~~
If i want the bullet attack the enemy and make it slow for a few second... Is it write the code in the bullet or enemy?
thankyou for your reply^^
Then only need to make a script inside the enemy
if (Receive a bullet)
{
StartCorutine( Slow(4.5f) );
}
[....]
IEnumerator Slow(float SlowTime)
{
Apply Slow to gameObject;
yield return new WaitForSeconds(SlowTime);
Remove slow fromObject;
}
Bye!
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
iPhone UI Components? 2 Answers
How to get the first component that implements a specific interface ? 4 Answers
Object reference not set to instance of an object 2 Answers
Using static typing for a Component 1 Answer