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. Dismiss Notice

Running start function more then once

Discussion in 'Scripting' started by tookydo, Aug 27, 2014.

  1. tookydo

    tookydo

    Joined:
    Jul 15, 2014
    Posts:
    75
    I have a multiplayer game and the function OnPlayerConnected isn't working when attached the player. In this case the code to initialize the player has to be on the player. Is there a way for every time a player gets instantiated that the player would run the start function for himself. Is there a way I could do that?
     
  2. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Do you mean something like this?

    Code (csharp):
    1. myPlayer = instantiate(playerPrefab, position, rotation);
    2. myPlayer.GetComponent<myPlayerScript>().RunThisFunction();
     
    tookydo likes this.
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    There's nothing stopping you from making Start public and calling it yourself...
     
  4. tookydo

    tookydo

    Joined:
    Jul 15, 2014
    Posts:
    75
    Code (CSharp):
    1. private void SpawnPlayer()    {
    2.         // Spawning the player. By using Network.Instantiate instead of just Instantiate, everyone on the server will have the player object instantiated (Though control should be reserved only to the owner of the NetworkView)
    3.         int randomSpawn = Random.Range(0, spawnZones.Length-1);    // We're choosing random spawn points from an array just for convenience here - you can handle this how you like, just keep in mind: Network.Instantiate creates the object on everyone's end
    4.         GameObject networkPlayer = Network.Instantiate(playerObject, spawnZones[randomSpawn].transform.position, Quaternion.identity, 0) as GameObject;
    5.         networkPlayer.networkView.RPC("SetUsername", RPCMode.AllBuffered, username);
    6.         networkPlayer.GetComponent<"Player">().PlayerInit();
    7.     }
    Yeah that is what I just tried and it says unexpe
    It is a multiplayer game just so you know and this is the code to spawn the player:
     
  5. tookydo

    tookydo

    Joined:
    Jul 15, 2014
    Posts:
    75
    Code (CSharp):
    1. private void SpawnPlayer()    {
    2.         // Spawning the player. By using Network.Instantiate instead of just Instantiate, everyone on the server will have the player object instantiated (Though control should be reserved only to the owner of the NetworkView)
    3.         int randomSpawn = Random.Range(0, spawnZones.Length-1);    // We're choosing random spawn points from an array just for convenience here - you can handle this how you like, just keep in mind: Network.Instantiate creates the object on everyone's end
    4.         GameObject networkPlayer = Network.Instantiate(playerObject, spawnZones[randomSpawn].transform.position, Quaternion.identity, 0) as GameObject;
    5.         networkPlayer.networkView.RPC("SetUsername", RPCMode.AllBuffered, username);
    6.         networkPlayer.GetComponent<"Player">().PlayerInit();
    7.     }

    This is my code
     
  6. image28

    image28

    Joined:
    Jul 17, 2013
    Posts:
    457
    Remove the Quotes from .GetComponent<"Player">.... and also if you want the the function to run on all the clients you may want to put the function call inside a RPC.