- Home /
boolean var generated in update not accessible
Hi, I have a problem with a respawn script!
I use a var called canSpawn that in Start() function is false. When a player is killed I use this code to wait x seconds
deadTime = respawnTimer += Time.time;
This code is placed in a RPC function.
My update function is this:
if (needRespawn)
{
if (deadTime < Time.time)
{
Debug.Log("can spawn");
canSpawn = true;
}
}
Debug.Log("can spawn"); is printed so I fiugre that canSpawn is true. The problem is that if I place an if statement like this: if (canSpawn) in the RPC function (the same of the deadTime = respawnTimer += Time.time; code), this if is never called.
Why?
Thanks a lot,
Marco
Answer by asafsitner · Jan 05, 2012 at 07:18 PM
It happens because of scope. A variable gets disposed of every time the program exits the block it was declared in. If you want your variable to be accessible from everywhere in your script, declare it at class level.