#Stopping the update function of a script

1 messages Ā· Page 1 of 1 (latest)

quaint osprey
#

What I would recommend doing (although there is probably a simpler way) is adding a bool that stops the update function when the bool is false

#

So maybe do something like this:


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

public class GameManager : MonoBehaviour
{
    public GameObject gameOverCanvas;
public bool timeStopped;
    // Start is called before the first frame update
    void Start()
    {
        Time.timeScale = 1;
        timeStopped = false;
    }

    public void GameOver()
    {
        gameOverCanvas.SetActive(true);
        Time.timeScale = 0;
         timeStopped = true;
    }

    public void Replay()
    {
        SceneManager.LoadScene(0);
    }
}
#

And then in your token script, do something like

void Update()
{
   if (GameManager.timeStopped = false)
{
   transform.position += Vector3.down * speed;
}
}```
#

@gloomy gull

gloomy gull
#

thanks just saw this im trying rn

#

so i changed the token script code to this

#

if (gameManager.timeStopped == false)
{
transform.position += Vector3.down * speed;
}

#

and at the top -- public GameManager gameManager;

#

but i get a compiler error on unity saying:

#

@quaint osprey

quaint osprey
gloomy gull
#

which token

#

theres gold one which is meant to be a token

#

and theres a bomb but i just used a silver token

quaint osprey
#

Wait a minute

#

It wants you to put something in the ā€œItem Behaviorā€ script

#

It’s looking for a game manager component

gloomy gull
#

i cant add anything there

#

it just wont let me

quaint osprey
#

Did you add that part of the script

#

Oh wait

gloomy gull
quaint osprey
#

Did you add ā€œpublic GameManager gameManagerā€to the script?

gloomy gull
#

yeah otherwise the(gameManager.timeStopped == false) wouldnt work?

quaint osprey
#

Change it to ā€œpublic script gameManagerā€ real quick

gloomy gull
#

doesnt work

quaint osprey
#

Yeah I thought so

#

Wait I know the issue

#

Oh shit hey sam

minor spruce
#

Oh hey

#

If u wanna stop the update function u could just make a bool and check if bool is true in the update function. Then you can just change the bool to stop all of the code in update from running

#

Ion even know if the question has been answered yet but yh

quaint osprey
minor spruce
#

Oh wait thats exatly what u said

#

Mb

quaint osprey
#

The issue is that ā€œGameManagerā€ is the name of the script that we want to pause, but ā€œGameManagerā€ is also a component, so we need to make it look for the script instead of the component

gloomy gull
#

how would that change it

#

ohh

quaint osprey
#

We don’t want to break anything

gloomy gull
#

but isnt the whole point of naming it GameManager is because it is a gamemanaer

minor spruce
#

Im not sure that it matters

quaint osprey
#

But yeah rename the script to like ā€œgame-managerā€ or something

quaint osprey
minor spruce
#

Or just like

#

ManageGame

#

Or smth

quaint osprey
#

Literally anything but what it is

#

And then in your item behavior function, write ā€œpublic ManageGame gameManagerā€

#

Hopefully that makes sense

gloomy gull
#

okay one sec

#

now i changed it to Manager

#

i cant add the script to any of the prefabs