Home > Enterprise >  Player prefeb are same whenever he instantiate but master car is qork fine
Player prefeb are same whenever he instantiate but master car is qork fine

Time:11-18

I want the car spawn selected by the player.......

but the problem is whenever the other local player select. the all car are same (mean player2 and player3).....

but the there is no issue with the masterclient car (mean host player) the car are same which he selected......

here is my code.....

the both script are in defferent scene....


    public GameObject Player;

    public GameObject Player2;

    public GameObject Player3;

        Vector2 randomPosition = new Vector2(Random.Range(minx, maxX), Random.Range(minY, maxY));

        string car = PlayerPrefs.GetString("getcar");

        if(car== "lamborgini")

        {

            PhotonNetwork.Instantiate(Player.name, randomPosition, Quaternion.identity);

        }

        else if (car == "lam")

        {

            PhotonNetwork.Instantiate(Player2.name, randomPosition, Quaternion.identity);

        }  

        else if (car == "gini" )

        {

            PhotonNetwork.Instantiate(Player3.name, randomPosition, Quaternion.identity);

        } 

        else 

        {

            PhotonNetwork.Instantiate(Player3.name, randomPosition, Quaternion.identity);

        }

The below code in which the player select the car through name enter image description here

CodePudding user response:

string car = PlayerPrefs.GetString("getcar"); gets the local value. From your code, it's not clear how it works, but you have two (maybe more, but these two came to my mind right now) meaningful options:

  • In the lobby when everyone selected a car (and saves it locally), all clients send a ready signal to the Host (through PunRPC for PhotonNetwork.MasterClient, you need to track this on the Host side), enable the 'Start Game' option (or the Host can start the game anytime but in this case, you have to add a default selection value), and after PhotonNetwork.LoadScene() everyone sends their selected car (id, name, whatever) to everyone also with PunRPC with RpcTarget.AllBuffered.
  • Or everyone sends their selection in the menu through PunRPC, the Host stores them, and after PhotonNetwork.LoadScene() when the players are joining, the Host sends to everyone the selected values (also PunRPC, and should be RpcTarget.AllBuffered).

Both cases RpcTarget.AllBuffered should be used if someone connects a little bit later.

CodePudding user response:

Problem solve boyzzz I use static variable y to get or set the value other than the player prefs I dont know why the playerprefs can't update everytime the player spawn Thats why I use static variable Thankx for helping me guyz

  • Related