Multiplayer pass int from lobby to game scene
So I'm currently working on my first multiplayer game and I'm stuck. I have a character selection screen in a lobby but I'm not sure how to get the selected character (an int) into the actual game scene. How can I send a value and the player that sent it into the game scene from the lobby?
Answer by Statement · Nov 01, 2015 at 01:58 PM
The simplest way is to just make a static variable.
public static class Globals {
public static int selectedCharacter;
}
It'll stick between levels and is accessible from anywhere without requiring a reference.
void OnClick() {
Globals.selectedCharacter = 5;
}
Load level...
void Awake() {
LoadCharacter(Globals.selectedCharacter);
}
If you need more information, you can add more static variables. If you grow tired of having a bunch of static variables (perhaps you keep adding lots of variables but it gets messy because some of them belong to one another while others dont), create a class to structure your data and make Globals reference an instance of your class.
You can also create a game object that has components that carry data. You can call DontDestroyOnLoad(gameObject) in Awake to keep it even when the level changes.
You can also have static variables in the classes that actually require them.
public class $$anonymous$$enuHandler : $$anonymous$$onoBehaviour
{
public void OnClick()
{
$$anonymous$$yPlayerScript.selectedCharacter = 5;
Application.LoadLevel("SomeLevel");
}
}
public class $$anonymous$$yPlayerScript : $$anonymous$$onoBehaviour
{
public static int selectedCharacter;
void Awake()
{
LoadCharacter(selectedCharacter);
}
}
Your answer
Follow this Question
Related Questions
Multiplayer - Error when rejoining room 1 Answer
Online client code execution 0 Answers
Multiplayer vehicle game - physics or not to physics 0 Answers
Match making in Photon 0 Answers