Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gaia2222 · Sep 26, 2018 at 05:56 PM · componentdelay

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...

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gaia2222 · Sep 26, 2018 at 07:01 PM 0
Share

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^^

avatar image tormentoarmagedoom gaia2222 · Sep 26, 2018 at 10:16 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

91 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges