Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 MrJCSmith · Sep 20, 2014 at 10:20 PM · c#invokerepeating

Invoke Repeating not incrementing a resource

Hi all,

I've run into a snag with one of my scripts. This script is supposed to increment a resource by a certain number every so often and then stop when the resource limit has been reached.

The script is attached to a building that when constructed will activate the attached script. I attached a debug log that said it had incremented the resource once and then was never heard from again. It also didn't add the desired resource to the pool.

 using UnityEngine;
 using System.Collections;
 
 public class resourceTick : MonoBehaviour {
     
     private resourceGUI material;
     public int incrementBy = 2;
     public float prodTime = 1f;
     
     
     void Start(){
                                 
         InvokeRepeating("resourceGain", 1, prodTime);
     }
     
     void resourceGain(){
 
         material = GameObject.Find ("Player").GetComponent<resourceGUI>();
         
         int maximumMetal = material.maxMetal;
 
         material = GameObject.Find("Player").GetComponent<resourceGUI>();
         int curMetal = material.currentMetal;
         
         if (curMetal < maximumMetal){
             curMetal += incrementBy;
             Debug.Log("Creating Metal: " + curMetal);
         }
         if(curMetal >= maximumMetal){
             curMetal = maximumMetal;
             CancelInvoke();
         }
     }
     
 }
 

I'm new to programming so I no doubt did something out of order.

Any help is greatly appreciated.

Comment
Add comment · Show 2
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 BDX777 · Sep 20, 2014 at 10:31 PM 0
Share

I think this is probably because you are using void Start () but I'm no expert on Invoke.

avatar image Eric5h5 · Sep 21, 2014 at 12:52 AM 0
Share

Start is a very common place for using InvokeRepeating.

2 Replies

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

Answer by Anxo · Sep 20, 2014 at 11:47 PM

I would check 2 things,

  1. put a debug in the clause that cancels the invoke or simply comment the cancel out and see if the invoke repeats then.

  2. Check you have overwritten the public variable for repeat time in the inspector. So you may thing its 1 but it may be 100 in the inspector.

Comment
Add comment · Show 4 · 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 Anxo · Sep 21, 2014 at 12:02 AM 0
Share

Damit, wrote another answer but can't answer twice and it took a while to write! Try this, clean for errors firs as I am not using unity atm but the logic should work.

 void OnEnable(){
    StartCoroutine("GeneradeGoods");
 }
 void OnDisable(){
  StopCoroutine("GeneradeGoods");
 }
 
 IEnumirator GeneradeGoods(){
       material = GameObject.Find ("Player").GetComponent<resourceGUI>();
         int maximum$$anonymous$$etal = material.max$$anonymous$$etal;
         int cur$$anonymous$$etal = material.current$$anonymous$$etal;
         while (true){
             yield return new WaitForSeconds(prodTime);
              while(cur$$anonymous$$etal >= maximum$$anonymous$$etal) yield return null;
             cur$$anonymous$$etal += incrementBy;
         
         }
 
 }
avatar image MrJCSmith · Sep 21, 2014 at 07:13 PM 0
Share

Hi Anxo, I had thought about using a coroutine but I'm still just going through some of the tutorials about them. Do you $$anonymous$$d explaining a little about how the above works?

avatar image Anxo · Sep 22, 2014 at 11:48 AM 0
Share

Hi $$anonymous$$rJCSmith, placing debug.log in the right places should help you find the issue but I think Eric's answer might be your problem.

The Coroutine I wrote works like this,

set material, declare and set maximum$$anonymous$$etal declare and set cur$$anonymous$$etal while true(true is always true, so while always) loop through the following code. wait for a prodTime before moving on to the next line. if curent metal is more than or = to maxmetal, wait till that is no longer the case. Then move on to the next line, cur$$anonymous$$etal+= incrementBy (bc we loop indefinatly...)Go back up to wait for prodTime, and follow instructions under the while(true)

avatar image MrJCSmith · Sep 23, 2014 at 07:51 PM 0
Share

Hi Anxo, thank you for your help on this one and taking the time to explain how the coroutine works. Appreciate it!

avatar image
0

Answer by Eric5h5 · Sep 21, 2014 at 12:55 AM

You declare a local variable called "curMetal" but don't really do anything with it. I don't know where the resource is stored, but wherever it is, it's untouched and will never increase.

Comment
Add comment · Show 3 · 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 MrJCSmith · Sep 21, 2014 at 07:18 PM 0
Share

Hi Eric, thank you for replying. Is the first 'if' statement not using the ints referenced above it?

I had set up cur$$anonymous$$etal to equal current metal in my GUI script. Did I call the variable incorrectly?

avatar image Anxo · Sep 22, 2014 at 11:42 AM 0
Share

What Eric is saying here is that your resourcesGain $$anonymous$$ethod creates a local variable, int cur$$anonymous$$etal = material.current$$anonymous$$etal; and later you change that variable's value to cur$$anonymous$$etal += incrementBy; But you never do anything with the variable, you just create it. You do not have something like, material.current$$anonymous$$etal = cur$$anonymous$$etal ;

avatar image MrJCSmith · Sep 23, 2014 at 07:52 PM 0
Share

Hi Anxo and Eric. It wouldn't let me mark both answers as correct but you both gave excellent advice. It was also correct in that I wasn't correctly setting what to increment. Thank you!

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Using invokerepeatng in update 4 Answers

How do I replace Invoke Repeating to allow a repeat rate change 1 Answer

Using InvokeRepeating in the Update method? 2 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