Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Players number

Discussion in 'Getting Started' started by littlewombat, Feb 18, 2015.

  1. littlewombat

    littlewombat

    Joined:
    Feb 11, 2015
    Posts:
    13
    Hello, in my game are max 8 people on map. I want to give number (first free from 1-8) for every player but the problem is that I don't know how to make one global object with this numbers. Actually i got (example for max 3players on map):
    in script in gameobject in scene:
    public static bool one=false;
    public static bool two=false;
    public static bool three=false;

    [RPC] void info(int number){
    if (number== 1)
    scriptsname.one= true;
    if (number== 2)
    scriptsname.two= true;
    if (number== 3)
    scriptsname.three= true;
    }

    then in scripts when create players:

    if (scriptname.one== false) {
    playersnumber= 1;
    GameObject.Find ("objectsname").GetComponent<PhotonView> ().RPC ("info", PhotonTargets.All, playersnumber);
    else if (scriptname.two== false) {
    playersnumber= 2;
    GameObject.Find ("objectsname").GetComponent<PhotonView> ().RPC ("info", PhotonTargets.All, playersnumber);
    else if (scriptname.three== false) {
    playersnumber= 3;
    GameObject.Find ("objectsname").GetComponent<PhotonView> ().RPC ("info", PhotonTargets.All, playersnumber);
    }

    problem is that every player got number "1" and only "one" in "scriptname" is true, probably because "gameobject" is not global and everyone got different one. What should I do?
     
  2. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    557
    Congratulations, you are about to enter the marvellous world of static members! Google can tell you in much more detail, but in a nutshell static members are variables or functions that are not dependant on - and don't require an instance of - a class object. C# is particularly nice about them, allowing you to mix static and non-static members almost seamlessly. A quick example:

    Code (csharp):
    1. class Dog : MonoBehaviour {
    2.     static int numDogs = 0;
    3.  
    4.     void Awake(){
    5.         numDogs++;
    6.         Debug.Log("There are " + numDogs + "dogs!");
    7.     }
    8. }
    9.  
    10. //elsehwere:
    11. Instantiate(dogPrefab);
    12. Instantiate(dogPrefab);
    13. Instantiate(dogPrefab);
    14.  
    15. //output:
    16. "There are 1 dogs!"
    17. "There are 2 dogs!"
    18. "There are 3 dogs!"
    Good luck!
     
  3. littlewombat

    littlewombat

    Joined:
    Feb 11, 2015
    Posts:
    13
    mhm this is not good solution, I have to send numbers to masterclient then check whether there are free or not