x


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

more ▼

asked Jan 05 '12 at 03:19 PM

marf gravatar image

marf
16 5 6 8

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Jan 05 '12 at 07:18 PM

asafsitner gravatar image

asafsitner
2.4k 2 8 19

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x681
x494
x237
x183
x34

asked: Jan 05 '12 at 03:19 PM

Seen: 501 times

Last Updated: Jan 05 '12 at 07:19 PM