unity-game-engine

Check if spawn is empty


I have two spawn spots where a player will show up upon connection.

I need that, when one of the players connects on one of the spots, any other player will always spawn at the other spot.

Here is the code I'm using:

using UnityEngine;
using System.Collections;

public class SpawnSpot : MonoBehaviour {

public int teamId=0;
public GameObject[] Spots; //Drag the spots in here (In the editor)
bool[] OccupiedSpawnSpots;

//Using Photon Networking
void OnJoinedRoom()
{
    //Request the recent OccupiedSpawnSpots List
    PhotonView.RPC("RequestList", PhotonTargets.MasterClient, PhotonNetwork.player); 
}

//In "RequestList" the MasterClient sends his List of the SpawnSpots 
//by calling "ReceiveList"
[RPC]
void RequestList(PhotonPlayer player)
{
    PhotonView.RPC("ReceiveList", PhotonTargets.All, player, OccupiedSpawnSpots);
}


[RPC]
void ReceiveList(PhotonPlayer Sender, bool[] ListOfMasterClient)
{
    OccupiedSpawnSpots = ListOfMasterClient;
    
    //Get the free one
    if (OccupiedSpawnSpots[0] == false)
    {
        //Spawn player at 0
        if (Sender == PhotonNetwork.player)
            PhotonNetwork.Instantiate("PlayerController", Spots[0].transform.position);
        OccupiedSpawnSpots[0] = true;
    } 
    else
    {
        //Spawn player at 1
        if (Sender == PhotonNetwork.player)
            PhotonNetwork.Instantiate("PlayerController", Spots[1].transform.position);
        OccupiedSpawnSpots[1] = true;
    }
}

The errors given are:

Assets/Scripts/SpawnSpot.cs(14,28): error CS0120: An object reference is required to access non-static member `PhotonView.RPC(string, PhotonPlayer, params object[])'

Assets/Scripts/SpawnSpot.cs(22,28): error CS0120: An object reference is required to access non-static member `PhotonView.RPC(string, PhotonPlayer, params object[])'

Assets/Scripts/SpawnSpot.cs(36,47): error CS1501: No overload for method Instantiate' takes 3' arguments

Assets/Scripts/SpawnSpot.cs(43,47): error CS1501: No overload for method Instantiate' takes 3' arguments

Thanks in advance, IC


Solution

  • It appears that you are trying to call an instance function using a static reference. instead of doing PhotonView.RPC("RequestList", PhotonTargets.MasterClient, PhotonNetwork.player); you need to create a reference to an PhatorView object, and then call the RPC function on it.

    public PhotonView photonView;
    void OnJoinedRoom()
    {
         if(photonView == null)
         {
             photonView = GetComponent<PhotonView>();
         }
    
         //Request the recent OccupiedSpawnSpots List
         PhotonView.RPC("RequestList", PhotonTargets.MasterClient, PhotonNetwork.player); 
    }
    

    you should have a PhotonView object on the same GameObject that this script is on, or assign a reference to a PhotonView in the editor.

    That should fix your problems, but I think you should look into compiler errors and how to to fix them. In Unity3D if you double click the error in the console it will take you to the line that isn't compiling. It also tends to give you a pretty good hint as to why it isn't compiling.

    your error was this

    "Assets/Scripts/SpawnSpot.cs(14,28): error CS0120: An object reference is required to access non-static member `PhotonView.RPC(string, PhotonPlayer, params object[])'"

    which means that you need an object in order to call this function.