Repeating a set of functions on bool check

Hi guys,

Want my hyperdrive to kick in on my space ship whenever the hyperdrive bool is true. It runs the process once perfectly, and unchecks true when its finished but clicking true again in the inspector doesnt start the process again. Or is there a better way of repeating function calls? I’m sure im doing something stupid that im just not seeing. Here is my code:

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

public class HyperDrive : MonoBehaviour
{
    public bool hyperspaceJump = false;
    private bool calloutCompleted = false;
    private bool speedUpStarSpeed = false;
    private bool slowDownStarSpeed = false;
    public AudioSource hyperdriveAnnouncement;
    public AudioSource hyperDriveSound;
    public ParticleSystem ps;

    void Update()
    {
        InitHyperspace();
        SpeedUpStars();
        SlowDownStars();
       
    }

    void InitHyperspace()
    {
        if (hyperspaceJump)
        {
            Invoke("StartHyperAnnouncement", 3);
        }
    }

    void SpeedUpStars()
    {
        if (speedUpStarSpeed)
        {
            ps.startSpeed += 0.9f;
            ps.startSize += 0.06f;
        }
    }

    void SlowDownStars()
    {
        if (slowDownStarSpeed)
        {
            ps.startSpeed = 10f;
            ps.startSize = 0.7f;
        }
    }

    void StartHyperAnnouncement()
    {
        if (!calloutCompleted)
        {
            hyperdriveAnnouncement.Play();
            calloutCompleted = true;
            StartCoroutine(StartHyperdriveSequence());
        }
    }

    IEnumerator StartHyperdriveSequence()
    {
        float waiting = 2;
        yield return new WaitForSeconds(waiting);
        hyperDriveSound.Play();
        speedUpStarSpeed = true;
        StartCoroutine(EndHyperdriveSequence());
    }

    IEnumerator EndHyperdriveSequence()
    {
        float waiting = 8;
        yield return new WaitForSeconds(waiting);
       
        speedUpStarSpeed = false;
        slowDownStarSpeed = true;
        hyperspaceJump = false;
    }
}

A few wonky things here, but the reason it’s not working again is that ‘calloutCompleted’ is blocking your second 'StartHyperAnnouncement".

Which is a good thing, because otherwise you would be invoking it hundreds or thousands of times over in every update.

Everything you’ve got there could be simplified into a single coroutine. Coroutines are specifically designed for managing a sequence of events distributed over time; there’s no need to burden your update loop or hand over to new coroutines (not when it’s as small and straightforward as this, and when there’s nothing to be gained from modularity):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HyperDrive : MonoBehaviour
{
    public bool hyperspaceJump = false;
    private bool inProgress = false;
    public AudioSource hyperdriveAnnouncement;
    public AudioSource hyperDriveSound;
    public ParticleSystem ps;

    void Update()
    {
        if (hyperspaceJump && !inProgress)
        {
            StartCoroutine(DoHyperspaceJump());
        }
    }

    IEnumerator DoHyperspaceJump()
    {
        inProgress = true;
        hyperspaceJump = false;

        yield return new WaitForSeconds(3);

        hyperdriveAnnouncement.Play();

        yield return new WaitForSeconds(2);

        hyperDriveSound.Play();

        //speed up stars
        float speedStarsUntil = Time.time + 8f;
        while (Time.time < speedStarsUntil)
        {
            ps.startSpeed += 0.9f;
            ps.startSize += 0.06f;
            yield return null;
        }

        //slow down stars
        ps.startSpeed = 10f;
        ps.startSize = 0.7f;

        inProgress = false;
    }
}

Thats great thanks, yes i did over complicate it. I see what you mean now. Thanks again.

Just in case you want to stop your coroutine - cache it, and bear in mind that WaitForSeconds will be affected if you change Time.timeScale