2
\$\begingroup\$

I have a slider in the pause menu which will have a min value of 1 and max of 5. When I change it, the InvokeRepeating function maintains the same speed, but Debug.Log shows current slider value.

How can I change the InvokeRepeating rate?

Player.m1 is a float; when I give 5f or something like that to InvokeRepeating it works fine.

    using UnityEngine;

    public class Gen1 : MonoBehaviour
    {
        public GameObject rocks;

        int scr = 0;
        public int m=0;
        float n11=Player.m1;
        float sp;

        // Use this for initialization
        void Start()
        {
            //if (Player.m1 == 5 || Player.m1==4) {
            InvokeRepeating ("CreateObstacle", Player.m1,5f);//Here speed handels
            //} else {
                //InvokeRepeating ("CreateObstacle", 4f, 5f);//Here speed handels
            //}
            Debug.Log ("current speed is afad"+Player.m1);
        }

        void Update()
        {
            //InvokeRepeating ("CreateObstacle", 4f, Player.m1); //Here speed handels
            //Debug.Log ("current speed is afad"+Player.m1);
        }

        void OnGui()
        {
            GUI.color = Color.black;
            GUILayout.Label ("Score is" + scr.ToString ());
        }

        void CreateObstacle()
        {
            Instantiate(rocks);
            Player.score += 1;
            Debug.Log ("current speed is "+sp);

        }

        void OnBecameInvisible() { 
            // Destroy the bullet 
            Destroy(this.rocks);
        } 
    }
```
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

Once an InvokeRepeating is started, you can't change the repeat rate. You have to Cancel the Invoke and start it again.... Or, you use the power of coroutines! Invoke and InvokeRepeating should be removed from the Unity API IMO...

public float sp = 5f ;

void Start()
{
    StartCoroutine( CreateObstacle(Player.m1) ) ;
}

IEnumerator CreateObstacle( float time )
{
    yield return new WaitForSeconds(time) ;
    while( true )
    {
        Instantiate(rocks);
        Player.score += 1;
        Debug.Log ("current speed is "+sp);
        yield return new WaitForSeconds(sp) ;
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ Thanks a lot it works but i made some changes \$\endgroup\$
    – GB Golden
    Apr 10, 2017 at 8:07
  • \$\begingroup\$ [code]yield return new WaitForSeconds (time); while (true) { Instantiate (rocks); Player.score += 1; Debug.Log ("current speed is " + sp); if (Player.m1 == 5) yield return new WaitForSeconds (1); else if(Player.m1==4) yield return new WaitForSeconds (1.5f); else if(Player.m1==3) yield return new WaitForSeconds (2); else if(Player.m1==2) yield return new WaitForSeconds (3); else yield return new WaitForSeconds (4); Debug.Log ("Setting speed"); } }[code] \$\endgroup\$
    – GB Golden
    Apr 10, 2017 at 8:08
  • \$\begingroup\$ Personally, I don't like coroutines either. But until we have a better event driven solution, that's our best bet. \$\endgroup\$ Apr 11, 2017 at 12:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .