Search Unity

[Fixed]Spawn Ignoring Assigned Transform Position

Discussion in 'Multiplayer' started by SkyTech6, Mar 11, 2016.

  1. SkyTech6

    SkyTech6

    Joined:
    Jul 14, 2015
    Posts:
    151
    Hey, I have two players in my game, using a single prefab. I use a switch statement to keep track of which player is Player 1 and which one is Player 2.

    I then have two spawn points set in the game that the players are to spawn at. Player 1 always spawns at the correct location. However, player 2 always spawns at 0,0,0 regardless of the fact it's assigned spawn point 2.

    If someone could look over my script and point out what I did wrong, I would highly appreciate it.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System.Collections;
    4.  
    5. public class NetworkPlayer : NetworkBehaviour
    6. {
    7.     [SerializeField]
    8.     Behaviour[] componentsToDisable;
    9.  
    10.     GameObject gm;
    11.  
    12.     //SpawnPoints
    13.     public GameObject P1Spawn;
    14.     public GameObject P2Spawn;
    15.     public GameObject P3Spawn;
    16.     public GameObject P4Spawn;
    17.  
    18.     void Awake()
    19.     {
    20.         gm = GameObject.Find ("gameManager");
    21.         P1Spawn = GameObject.Find ("player1Spawn");
    22.         P2Spawn = GameObject.Find ("player2Spawn");
    23.         P3Spawn = GameObject.Find ("player3Spawn");
    24.         P4Spawn = GameObject.Find ("player4Spawn");
    25.     }
    26.  
    27.     void Start ()
    28.     {
    29.         Debug.Log ("Local: " + isLocalPlayer);
    30.         if (!isLocalPlayer)
    31.         {
    32.             for (int i = 0; i < componentsToDisable.Length; i++)
    33.             {
    34.                 componentsToDisable[i].enabled = false;
    35.             }
    36.         }
    37.         else
    38.         {
    39.             CmdSpawnPlayers ();
    40.         }
    41.     }
    42.  
    43.     [Command]
    44.     void CmdSpawnPlayers()
    45.     {
    46.         GameManager manager = gm.GetComponent<GameManager>();
    47.  
    48.         switch (manager.numberOfPlayers)
    49.         {
    50.         case 0:
    51.             this.transform.position = P1Spawn.transform.position;
    52.             this.GetComponent<Player> ().startAngle = 45f;
    53.             this.tag = "Player1";
    54.             this.name = "Player1";
    55.             manager.numberOfPlayers++;
    56.             Debug.Log ("Player1");
    57.             break;
    58.         case 1:
    59.             this.transform.position = P2Spawn.transform.position;
    60.             this.GetComponent<Player> ().startAngle = 225f;
    61.             this.tag = "Player2";
    62.             this.name = "Player2";
    63.             manager.numberOfPlayers++;
    64.             Debug.Log ("Player2");
    65.             break;
    66.         }
    67.     }
    68. }
    Also, I'll note the the debugs are giving the exact responses they should... case 1 is called when player 2 connects, spawning a Player 2 instance of the prefab.
     
    Last edited: Mar 11, 2016
  2. SkyTech6

    SkyTech6

    Joined:
    Jul 14, 2015
    Posts:
    151
    Removed [Command] and now it works... haha....