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 /
  • Help Room /
avatar image
0
Question by Gymhgy456 · Jul 25, 2017 at 04:32 AM · scripting problemfunctionmethod

Function won't be called

I have a method that takes the amount of time that has passed from the Timer script that I have and decrease the spawn time by seconds. But when I run, the float does not decrease.

My spawn code

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Cubes : MonoBehaviour {
     public float delay = 2f;
     public GameObject cubes;
     public Timer timer;
 
     // Use this for initialization
     void Start () {
         InvokeRepeating ("Spawn", delay, delay);
         GameObject time = GameObject.Find("Timer");
         timer = time.GetComponent<Timer>();
     }
     
     // Update is called once per frame
     void Spawn () {
         Instantiate(cubes, new Vector3 (Random.Range(-6, 6),10,0),Quaternion.identity);
     }
 
     void Update ()
     {
         if (timer.timer % 10 == 0) {
             MinusDelay ();
         }
     }
 
     public void MinusDelay () {
         if (delay <= 0.1f) {
             delay -= 0.1f;
         }
     }
 }
 

Timer code (The Cubes script is attached to the MainCamera)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Timer : MonoBehaviour {
 
     public float seconds = 0.0f;
     public float timer = 0.0f;
     public int minuteCount= 0;
     public int hourCount = 0;
     public Text text;
     public GameObject delayer;
 
     // Use this for initialization
     void Start () {
         text = GetComponent<Text> ();
     }
 
     // Update is called once per frame
     void Update () {
         timer += Time.deltaTime;
         seconds += Time.deltaTime;
         text.text = hourCount + ":" + minuteCount + ":" + seconds;
 
         if (seconds >= 60) {
             minuteCount++;
             seconds = 0;
         } 
         else if (minuteCount >= 60) {
             hourCount++;
             minuteCount = 0;
         }
 
     }
 
     void DeActivate () {
         gameObject.SetActive (false);
     }
 }
 

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
3
Best Answer

Answer by Iarus · Jul 25, 2017 at 05:57 AM

Hi, I'm a beginner at Unity, but I might be able to help.

I can see 2 problems in your Cubes script:

  • In your Update method, you do if (timer.timer % 10 == 0), the timer variable is a float and the chances that it gets to an exact multiple of 10 are very slim. You probably get 10.1 or 30.0002. To fix that, you could use Mathf.ApproximatlyEquals(timer.timer % 10, 0, SOME_VALUE), I don't know the exact function name. There is a parameter that is used to choose how close the 2 numbers need to be to be considered the same. Make it too small and you risk jumping over and not calling MinusDelay(). Make it too big and you risk calling MinusDelay() multiple times in a row. You may want instead to change the type of Timer.seconds to an int and use that in Cubes.Update(). then set seconds = (int)timer; in Timer.Update() instead of seconds += Time.deltaTime.

  • In C#, when passing a value type variable to a function, it's value is copied to the function and modification to the original are not taken into account (not updated) when in the function, here InvokeRepeating(...). A value type is an int, a float, double, string or any struct. Reference types are classes. So if you call InvokeRepeating ("Spawn", delay, delay) with delay = 2, then it repeat at 2 seconds interval forever, even if you change delay to 0.5. Also be careful to not call Invoke and InvokeRepeating with negative values or zero. You could control spawning all in Cubes.Update(), but that's boring or you could look into coroutines. Replace InvokeRepeating ("Spawn", delay, delay); with StartCoroutine(SpawnCoroutine()); Then add this method:

     private IEnumerator SpawnCoroutine()
       {
          yield return new WaitForSeconds(delay);
       
         while(keepOnSpawning)
         {
           Spawn();
           yield return new WaitForSeconds(delay);
         }
       }
    
    
    

I hope I could help

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 Gymhgy456 · Jul 25, 2017 at 04:49 PM 0
Share

So do I have to have a function called keepOnSpawning? Sorry, I don't really get coroutines

avatar image Bunny83 · Jul 25, 2017 at 05:00 PM 0
Share

Well analysed. +1

@Gymhgy456: "keepOnSpawning" is just a "bool" so you can stop the coroutine. It should be set to "true".

avatar image Gymhgy456 Bunny83 · Jul 25, 2017 at 06:11 PM 0
Share

So ins$$anonymous$$d can I do while (true) too?

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

110 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

Related Questions

Script not returning value to other script 1 Answer

Following round planet tutorial, line of code not working? 2 Answers

What go inside the brackets in voids? 1 Answer

[SOLVED]My code line don't execute 1 Answer

method return type is incompatible 0 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