• 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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

how to stop object before GO writes 1 Answer

How do I make a counter that goes up by 1 every second? 1 Answer

Object over terrain 2 Answers

How would I decrease a variable over time based on distance? 1 Answer

Wait before being able to press button again 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges