C# get variable from another script

Hi

I’ve two scripts

and second

I try access totalTime variable from RaceManager in StageManager script.

1 Like

Dunno if this is what is wrong but you could try changing

 racemanager = GetComponent<RaceManager>();

to

racemanager = gameObject.GetComponent<RaceManager>();

if the stage manager script is inside the same gameobject as the racemanager script… if it is not you will have to use GameObject.Find or FindWithTag like you did in the line above.

racemanager = GameObject.Find("Racemanager").GetComponent<RaceManager>();

if it fails to find the object or script when you try to set totalTime in the next line it should get a NullReference exception.

Also watch out for case sensitivity.

1 Like

Make public static float totalTime;
Then you can acces AND change this variable globaly from any other script.
In your case call totalTime from StageManager script like this:
RaceManager.totalTime

if you change it like this: RaceManager.totalTime=5; it will be changed globally(both in RaceManager and StageManager script)

1 Like

Please don’t do that, it’s really a bad idea.

Use GetComponent as someone said on previous post.

1 Like

All my global variables are static and everything works perfectly. This would not be allowed if it warent useful.

Okay. Good luck with your project.

Likewise.

Instead of fighting each other, maybe it would be better to explain your position guys?

I presume what jvil tried to say is that using public static (aka global variables) is just bad programming habit. The main problem with global variables is that they violate OOP principles of encapsulation which is always not good. They can always result in unexpected problems with you code (side effects) and they make testing quite “painful”.

On the other hand there are examples when global variables are useful. One of those examples are constant values, which usually end up being global variables.

I hope this tread will help you in the future. :sunglasses:

4 Likes

This topic has already been discussed so many times.

Set all variables to static it’s a common pitfall for newbie developers.

There are good reasons for use static variables, but when you set all your variables into static it’s a sign you have no idea what you’re doing.

So, no matter if you told them about permanent memory consumption, data encapsulation, concurrency, make code easy to mantain, and more stuff, they will continue to do it.

Just check the code of any decent open-source app and see how many static variables they are using, then ask yourself why you’re using so many static variables and someone experienced developing apps don’t.

If any of your variable needs to be globaly accesed and changed, make it static. Use lock if neccesary. Simple as that.

1382322--70441--$Facepalm_227785.jpg

7 Likes

Take your time, no rush.

Ok, here is a valid question.
We have a global static variable “something”.
If script1 is accesing this variable the same time as script2 trys to change it, can script1 prevent script2 from changeing the variable.Anyone is welcome to answer.

The question is moot in the context of MonoBehaviour derivatives and a large chunk of Unity-specific programming anyway because the application is single-threaded. You’re skewing the argument away from the usage of static variables and towards the concept of thread safety.

So, it won`t prevent it. Thanks.
This was no argument, I really was worried :slight_smile:

The only time I use static variables is when I need to access them from a different scene… I know there is a better way, but I haven’t started using it yet. I try to keep those variables to a bare minimum…

Sure. Health value,inventory etc…
One could go through - get_componnent - methods to check for variables changes in other scripts and then updateing them constantly, but why do that when static variables give you a direct acces to modify these variables.

Because static variables don’t give you any encapsulation or state. Anything in your application could change them from anywhere at anytime and it’d be very difficult to track down. Generally speaking, jvil was correct - static variables are not ideal for situations like the ones you mentioned.

Anything from my application ?
If it`s my application then I decide what is public ,private ,static etc.
Anyway, I was reffering to single player applications.Generally speaking, it is best to use static variables in them.

This link is very useful!

http://unitygems.com/script-interaction1/

Check this out, it has simple and better solution about accessing variables from other class — http://aarlangdi.blogspot.com.au