Run a method outside of update

I created a method that simulates fire movement. This movement gets triggered from an external script but my problem is that I cannot start this method outside of Update. I need to call and start this method on Start because it’s too expensive to run this in each frame, and the Instantiate is too fast -it should instantiate slowly. The problem is that on Start doesn’t run.

Is there any way to run this method on start?

Here’s my sample code.

    [HideInInspector] public bool isMoving; // State variable that checks if the fire isMoving
    public GameObject fire; // the var that holds teh fire object
    [SerializeField] private float speed; // fire speed


    private void Update() {
        FireMoveCheck();
    }

    private void FireMoveCheck() {
        if (isMoving) {
            Invoke(nameof(MovingFire), 2);
        }
        else if (isMoving == false) {
            CancelInvoke();
        }
    }

    private void MovingFire() {
        var degrees = 0;
        degrees = GetCompassInput(degrees);
        gameObject.transform.position = transform.TransformPoint(speed / 3600, speed / 3600, 0);
        gameObject.transform.rotation = Quaternion.Euler(-90, 0, degrees + 45);
        var position = gameObject.transform.position;
        var rotation = gameObject.transform.rotation;
        Instantiate(fire, position, rotation);
    }

I’m not sure I fully unerstand the issue you are having, but have you tried InvokeRepeating?

1 Like

Try looking into Coroutines. They allow you to move at your own pace, using things like WaitForSeconds. In particular WaitUntil could be interesting for you if you want to react to input.

4 Likes

Have you thought about simply enable, and disable the script containing your Update method, when you want the FireMoveving stuff going on, or be paused?

1 Like

Sorry for the late reply. My problem is that if I move the FireMoveCheck on Start, it doesn’t run, as it runs inside Update. I’ll try the solution you suggest and I’ll let you know. Thanks!

Sorry for the late reply. I’ll check Coroutines, and I’ll let you know. Thanks a lot!

Sorry for the late reply. Hmm, I didn’t think of this approach. Maybe I should consider it. Thanks!

1 Like