Setting a different parent for each player

Options

I'm trying to make a game, where at the start of the game, every player is instantiated and assigned a next spawn point. I want the player's object to be a child of this spawn point. I tried to make an RPC, but it seems like it gives every player the same parent. In the photo is my recent attempt.

How do I set a different parent for each player as they get their object in the scene?

Best Answer

  • eMDeMeister
    Answer ✓
    Options

    Solved it. It was easier than I initially thought actually. Found out I can just look up the actor number that is set on the current player object's Photon View.

    The code in the Start() method of the player prefab's Script:

    foreach (GameObject player in GameObject.FindGameObjectsWithTag("PlayerInstance"))

        {

          PlayerScript playerScript = player.GetComponent<PlayerScript>();

          player.transform.SetParent(spawnPoints[(playerScript.view.OwnerActorNr - 1)].transform);

          player.transform.localPosition = new Vector3(0, 0, 0);

        }

Answers

  • eMDeMeister
    Options

    If i try to do "SetParents" method, if view is mine, then that one player which is local gets assigned a parent and all the other ones do not. I want for every player to have the same scene and player parent allocation.

  • eMDeMeister
    Answer ✓
    Options

    Solved it. It was easier than I initially thought actually. Found out I can just look up the actor number that is set on the current player object's Photon View.

    The code in the Start() method of the player prefab's Script:

    foreach (GameObject player in GameObject.FindGameObjectsWithTag("PlayerInstance"))

        {

          PlayerScript playerScript = player.GetComponent<PlayerScript>();

          player.transform.SetParent(spawnPoints[(playerScript.view.OwnerActorNr - 1)].transform);

          player.transform.localPosition = new Vector3(0, 0, 0);

        }