#Stopping the update function of a script
1 messages Ā· Page 1 of 1 (latest)
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
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
Can you show me the token prefab
which token
theres gold one which is meant to be a token
and theres a bomb but i just used a silver token
Wait a minute
It wants you to put something in the āItem Behaviorā script
Itās looking for a game manager component
Did you add āpublic GameManager gameManagerāto the script?
Change it to āpublic script gameManagerā real quick
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
Thatās what weāre doing bruh
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
Rename the script then
Iām just trying to think about the things that that could effect
We donāt want to break anything
but isnt the whole point of naming it GameManager is because it is a gamemanaer
Im not sure that it matters
But yeah rename the script to like āgame-managerā or something
It do
Exactly
Literally anything but what it is
And then in your item behavior function, write āpublic ManageGame gameManagerā
Hopefully that makes sense