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 /
This question was closed Oct 14, 2016 at 06:53 PM by rooster2 for the following reason:

i resolved the problem

avatar image
0
Question by rooster2 · Oct 14, 2016 at 05:12 PM · script.timewaitforsecondsovercollection

script not working :(

hi im tring to make a game but this scrpt isnt working its ment to be a gather over time recorse script so i gets ammo every 5 seconds then another piece till it gets to max but it desnt collect unless its in the update but i cant use the wait for seconds in update how can i fix

public int AmmoRec = 0;

 public int MaxAmmoRec = 500;

 // Use this for initialization
 void Start () {
 
 }
 
 // Update is called once per frame
 void Update () {
     Add ();
 }

 IEnumerator Add(){
     if (AmmoRec < MaxAmmoRec) {
         yield return new WaitForSeconds (5.0f);
         AmmoRec ++ ;
     } else if (AmmoRec == MaxAmmoRec) {
         AmmoRec = MaxAmmoRec;
     }
     Debug.Log (AmmoRec);

 }
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

2 Replies

  • Sort: 
avatar image
1

Answer by tanoshimi · Oct 14, 2016 at 05:25 PM

  public int AmmoRec = 0;
  public int MaxAmmoRec = 500;

  void Start () {
      InvokeRepeating("Add", 0f, 5f);
  }
  
  void Add(){
      if (AmmoRec < MaxAmmoRec) {
          AmmoRec ++ ;
     }
  }
Comment
Add comment · 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
0

Answer by Naphier · Oct 14, 2016 at 05:28 PM

Well, you could start by watching some tutorial videos on coding to learn a bit about how IEnumerators (coroutines) work in Unity. First, you need to call StartCoroutine(Add()) -- that will run the coroutine (do this in your start method, NOT in Update()). Second, the coroutine will need a loop (such as while) to continue executing in a loop. Or you could use InvokeRepeating on any void method.

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Time Manager Class Implementation Like Coroutine in Unity 1 Answer

Object over terrain 2 Answers

increase over time 1 Answer

How to change value of variable over period of time? 2 Answers

Loading Screen and Lerp 3 Answers


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