- Home /
check if the bool is true from other script c#
hi . i want to make a popup window when the character die with UI . i made a pause bool in character script and it works . in canvas script i want to say when pause is true ( or time.deltatime = 0 ) show the canvas . i wrote this in canvas script but it doesn't work .
void Start ()
{
gameObject.SetActive (false);
}
void Update ()
{
if(GameObject.Find ("ball").GetComponent<ball_script>().pause)
{
gameObject.SetActive (true);
}
}
}
and this is part of the script for my character:
public bool pause = false;
void Update()
{
if(dead)
{
deathCooldown -= Time.deltaTime;
if(deathCooldown <= 0)
{
if(pause = true )
{
Time.timeScale = 0;
}
}
}
}
ok i have tried other things but i always get some error . what should i write in my character script to find the canvas and set active to true ?
I have written this in my character script but gives error :
if(pause = true )
{
Time.timeScale = 0;
GameObject.Find ("Canvas").GetComponent<popupDead>().gameObject.SetActive(true);
}
Answer by NoseKills · Feb 28, 2015 at 01:46 PM
If you do setActive(false) in Start(), Update is not automatically being called. You have to keep the UI GameObject active but prevent drawing the UI with another boolean or something.
Or you could call canvasObject.setActive(true) from your character script when he dies.
what should i write in my character script to find my canvas and set activer to true . right now i wrote this but gives error :
if(pause = true )
{
Time.timeScale = 0;
GameObject.Find ("Canvas").GetComponent<popupDead>().gameObject.SetActive(true);
}
Why not just have a reference to the canvas set in the inspector? This way you can use the same code to display activate different things.
Finding objects by name is slow, and it can create hard to track bugs if anyone changes the name of the objects (something really common).
If you still want to search for the Canvas, doing:
GameObject.Find ("Canvas").GetComponent<popupDead>().gameObject
is redundant, "GameObject.Find ("Canvas")" already returns the object, you don't need to get a component and then the gameObject for that component.
ok thanks , can you help me with making a refrence to the canvas set in the inspector , because it's hours i'm trying and i always get an error .
It would help if u told us what the error was. At least you seem to be trying to set pause
to true inside an if-clause: if (pause = true)
, you probably want it to be a boolean check: if (pause == true)
or just if (pause)
which works as well. Otherwise I can't spot anything obvious if "Canvas" and popupDead
are spelled correctly and that script is attached to that gameObject when the call is made.
the pause is working very good ,the problem is only in canvas when i say setactiv to false in start then in update it will not check my if (pause = true) . what is your suggestion to write insted of setactive to false ? void Start () { gameObject.SetActive (false);
}
void Update ()
{
if(GameObject.Find ("ball").GetComponent<ball_script>().pause)
{
Debug.Log("it works");
}
}