3
votes

I making a simple game for practice. I have 2 cube like 2 player. I generate a random number this is the name of the cube. I store this numbers in an array and i have not got problem with this. But when i made my game multiplayer i get some issue cuz when i join the second player i think the game regenerate the first player's number... this you can see on the picture! And this is the script which generete and add the number to name. And this script attached to the player prefab. enter image description here

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;


public class PlayerThings : NetworkBehaviour {

    public int player_name;
    public GameObject Number;

    void Start () {

        player_name = Random.Range (10, 99);
        this.name = player_name.ToString ();
        GameManager.numbers [player_name] = 1;
        Number.GetComponent<TextMesh> ().text = name;

    }

    // Update is called once per frame
    void Update () {

    }
}
2
you need a method of handing the client details over (eg number, position, colour whatever) on clients joiningBugFinder
And u can help me what method should i use? Or u can give me az example?antal1208
what code do you have to notify a client of another client joining it?BugFinder
Nothing, i have only this and one more script which contain an array(GameManager.number[]).antal1208
so you arent really joining 2 clients togetherBugFinder

2 Answers

0
votes

Both clients will run the script individually of each other which means the Random.Range code will run twice and get an individually random number, you need to think of a way to prevent both clients to generate random value.

If it's just an index I suggest using number of players+1 or similar which will be consistent.

EDIT: Or as @BugFinder wrote in the comments, send your Randomly generated number to the server and let the server distribute the name of the players to the clients.

0
votes

Both the players run their own instance of the game. So its possible you will have the same name. You will have to broadcast the names to each player when then join a room / lobby. Or have the server handle it.

For instance

  • User A joins the room
  • Server creates a unique name say a number 100
  • Send 100 as name to user 'A'
  • User B joins the room
  • Server increments the number by 1
  • New name is 101
  • Send 101 as name to user 'B'