How to acces a variable from another script

I found this on internet Referencing non static variables from another script? C# - Questions & Answers - Unity Discussions
And my question is next : what means these lines of code ?

  • GameObject thePlayer =GameObject.Find(“ThePlayer”);
  • PlayerScript playerScript = thePlayer.GetComponent();
  • playerScript.Health-=10.0f;

I did manage to acces the variable very easy by making it static and just writing ThatClass.variable
But those lines of code got my interest,if you can simply acces a variable just by ThatClass.variable,what those lines of code from above mean ?

a static member is the same value for every object of the class.

if you have two character, you can’t use a static member for the health because if you modify the value of the static member, it’s modified for every object.

So in the code above :

1 : you get the player object
2 : You get the script “PlayerScript”
3 : you modify the health of the specific player

i can take another exemple :

in my game everygun has the same damage so i can declare “damageGun” a static member.
My guns have different magazine size, so the “magazineSize” should be a not static member.

1 Like

O i get it thanks

1 Like