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 Flaymes · Oct 29, 2020 at 01:32 PM · coroutinestatic

Call methods on start of Application

I am trying to make a bank interest system that gives you 1.25x your overall amount of coins every 10 minutes. I was unable to make it that it did it outside of the game being open so I decided to make it just while you are playing the game. So when you start the game it starts a coroutine that makes it that 600 seconds it would give you interest. I am unable to make it that this coroutine is called from the start of tha game though, someone please try to help me, here is the script :

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Interest : MonoBehaviour { public static float fCoins; public static int Coins; public static bool displayTime = false; public static float seconds; public static string secondsTXT;

 [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
 static void OnBeforeSceneLoadRuntimeMethod()
 {
     StartCoroutine(InterestWait()); // I can not call other methods from within this method.
 }

 public void Start()
 {
     PlayerPrefs.SetFloat("fCoins", PlayerPrefs.GetInt("Coins"));
     fCoins = PlayerPrefs.GetFloat("fCoins");
     Coins = PlayerPrefs.GetInt("Coins");
 }

 public void Update()
 {
     if (displayTime)
     {
         Debug.Log(secondsTXT);
     }

     seconds += Time.deltaTime;

     secondsTXT = seconds.ToString("0 minutes and 00 seconds");
 }

 public void GiveInterest()
 {
     PlayerPrefs.SetFloat("fCoins", PlayerPrefs.GetFloat("fCoins") * 1.25f);
     PlayerPrefs.SetInt("Coins", Mathf.RoundToInt(PlayerPrefs.GetFloat("fCoins")));
 }

 public IEnumerator InterestWait()
 {
     yield return new WaitForSeconds(10f);
     GiveInterest();
     Debug.Log("Gave Interest");
 }

}

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

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

Answer by Strixie13 · Oct 29, 2020 at 01:43 PM

I would just start the coroutine in the start function. Remove the before scene runtime.

     public void Start()
     {
         PlayerPrefs.SetFloat("fCoins", PlayerPrefs.GetInt("Coins"));
         fCoins = PlayerPrefs.GetFloat("fCoins");
         Coins = PlayerPrefs.GetInt("Coins");
 
         StartCoroutine(InterestWait()); 
 
     }


Then fix it to be an infinite loop. The way you have it now the coroutine will only run once then exit. With the loop it will continue running until you call StopCoroutine().

     public IEnumerator InterestWait()
     {
         while(true)
         {
             yield return new WaitForSeconds(10f);
             GiveInterest();
             Debug.Log("Gave Interest");
         }
     }

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 outramjamie_unity · Oct 29, 2020 at 03:08 PM

The problem is you can't call any non-static methods from a static method and StartCorroutine/Invoke/InvokeRepeating are a non-static methods. I'd use InvokeRepeating in the Awake function and the DontDestroyOnLoad function to keep the game object loaded between scenes:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Interest : MonoBehaviour
 {
     public static float fCoins; 
     public static int Coins; 
     public static bool displayTime = false; 
     public static float seconds; 
     public static string secondsTXT;
 
     private void Awake()
     {
         DontDestroyOnLoad(gameObject);
         InvokeRepeating("GiveInterest", 10f, 10f);
     }
 
     private void Start()
     {
         PlayerPrefs.SetFloat("fCoins", PlayerPrefs.GetInt("Coins"));
         fCoins = PlayerPrefs.GetFloat("fCoins");
         Coins = PlayerPrefs.GetInt("Coins");
     }
     private void Update()
     {
         if (displayTime)
         {
             Debug.Log(secondsTXT);
         }
         seconds += Time.deltaTime;
         secondsTXT = seconds.ToString("0 minutes and 00 seconds");
     }
     private void GiveInterest()
     {
         PlayerPrefs.SetFloat("fCoins", PlayerPrefs.GetFloat("fCoins") * 1.25f);
         PlayerPrefs.SetInt("Coins", Mathf.RoundToInt(PlayerPrefs.GetFloat("fCoins")));
         Debug.Log("Gave Interest");
     }
 }
Comment
Add comment · Show 1 · 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 Flaymes · Oct 29, 2020 at 08:08 PM 0
Share

Although your answer was most likely correct too. I tested @Strixie13 's answer and it was exactly what I had been wanting.

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

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

How can I instantiate a prefab from a static function? 1 Answer

Static Coroutine being called endlessly 2 Answers

"Object reference required" error when using Start/StopCoroutine 1 Answer

Singleton instance accessed in coroutine always null 1 Answer

Best way to make melee AI damage player? 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