- Home /
C# networking - static variable
Hello guys,
I have a static variable for a simple networking game i'm making the represents a character's level that i'm displaying above their head.
The problem is that when I have two characters in the scene the other character displays the the first player's level. It looks like it just isnt sending the message correctly?
Here's the RPC I have set up: TimeText.currentOverAllLevel
void OnTriggerEnter(Collider other)
{
photonView.RPC("UpdateCharacterLevel", PhotonTargets.All, "2");
}
[RPC]
void UpdateCharacterLevel(int addLevel, PhotonMessageInfo info){
TimeText.currentOverAllLevel += addLevel;
Debug.Log ("All the other players should know!");
}
Thanks for any thoughts! Chris
Answer by Huacanacha · Nov 21, 2013 at 10:35 PM
If both players use the same script the value of the static variable will always be the same for both of them. That's the way statics work... 1 variable for the class, not 1 variable per instance. Convert it to an instance variable and you can have different values for each instance, or character in this case.
An instance variable would just be a private or public variable, correct?
No, accessors have nothing to do with static vs instance. An instance variable is created along with the object, static variables exist once for all objects that define it, even if you created a class that is going to be instantiated, but contains 2 static variables, everything that isn't static will exist as a instance of that instantiated object, the 2 static variables will exist just twice, once for each static and exist the same for all instances, if you change it in Enemy1 instance, then Enemy2 has that same change, for instance variables, you would have to change each instance's corresponding variable.
An instance variable is a variable defined in a class (not within a function) that isn't static. The variable access level is unrelated. Statics can have access modifiers (public, private etc) too.
Access modifier = who can access the variable (anywhere, inside class only etc) Static/non-static = does the class own the variable (static) or does an instantiated object own the variable (non-static)
Your answer
![](https://koobas.hobune.stream/wayback/20220613122437im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unity Networking RPC - Photon Cloud PUN 0 Answers
Photon Cloud, Multiplayer Damage script 0 Answers
C# How to have a toggle 1 Answer