Unity Fusion runner.spawn object only spawning in host screen

244 Views Asked by At

I am trying to spawn an object using fusion's runner.spawn method, The problem is when I Build and run the game with multiple users, the first user, that acts as hosts has the object pop up on the screen. Then if load another instance of the game, the object for user also pops on user 1's screen and nothing is visible on user 2's screen. I am using the AutoHostOrClient game mode.

I am very new to Unity dev and fusion, so still trying to figure out how it works. I do no need movement of the player object(prefab) as this is a playing cards game, and the object that i am trying to spawn is just the panel that will be holding the cards for each player. Below is my code for the NetworkManager:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Fusion;
using Fusion.Sockets;
using UnityEngine.SceneManagement;
using System.Threading.Tasks;
using NanoSockets;
using static UnityEngine.RuleTile.TilingRuleOutput;
using static Unity.Collections.Unicode;
using Unity.VisualScripting;

[RequireComponent(typeof(NetworkRunner))]
public class MainNetworkRunner : MonoBehaviour, INetworkRunnerCallbacks
{
    public NetworkObject playerHand;

    //class variables
    [HideInInspector] GameObject playerSpawnSpot, opponentSpawnSpot ;

    void Awake()
    {
        if (runner == null)
        {
            runner = gameObject.GetComponent<NetworkRunner>();
            cardsDeck = CardDatabase.cardList;
            //this.dealCards();
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        playerSpawnSpot  = GameObject.Find("PlayerSpot");
        opponentSpawnSpot = GameObject.Find("OpponentSpot1");

        InitializeNetworkRunner(runner, GameMode.AutoHostOrClient, NetAddress.Any(), SceneManager.GetActiveScene().buildIndex, null);
    }

    protected virtual Task InitializeNetworkRunner(NetworkRunner _runner, GameMode _gameMode, NetAddress _address, SceneRef _scene, Action<NetworkRunner> _initialized)
    {
        _runner.ProvideInput = true;

        Debug.Log("Session initializing");

        return _runner.StartGame(new StartGameArgs
        {
            GameMode = GameMode.AutoHostOrClient,
            Address = _address,
            Scene = _scene,
            SessionName = "SessionName",
            Initialized = _initialized,
            SceneManager = gameObject.AddComponent<NetworkSceneManagerDefault>()
        });
        //   PlayerCount = 3
    }

    void INetworkRunnerCallbacks.OnConnectedToServer(NetworkRunner runner)
    {
        Debug.Log("Connected to a server");
    }

    void INetworkRunnerCallbacks.OnConnectFailed(NetworkRunner runner, NetAddress remoteAddress, NetConnectFailedReason reason)
    {
        Debug.Log("Connection to server failed");
    }

    void INetworkRunnerCallbacks.OnConnectRequest(NetworkRunner runner, NetworkRunnerCallbackArgs.ConnectRequest request, byte[] token)
    {
        Debug.Log("Requesting connection to server");
    }

    void INetworkRunnerCallbacks.OnCustomAuthenticationResponse(NetworkRunner runner, Dictionary<string, object> data)
    {
    }

    void INetworkRunnerCallbacks.OnDisconnectedFromServer(NetworkRunner runner)
    {
    }

    void INetworkRunnerCallbacks.OnHostMigration(NetworkRunner runner, HostMigrationToken hostMigrationToken)
    {
    }

    void INetworkRunnerCallbacks.OnInput(NetworkRunner runner, NetworkInput input)
    {
    }

    void INetworkRunnerCallbacks.OnInputMissing(NetworkRunner runner, PlayerRef player, NetworkInput input)
    {
    }

    void INetworkRunnerCallbacks.OnPlayerJoined(NetworkRunner runner, PlayerRef player)
    {
        Debug.Log("Player joined  - " + player.PlayerId);


        NetworkObject playerHandObject = runner.Spawn(playerHand, playerSpawnSpot.transform.position);
        runner.SetPlayerObject(runner.LocalPlayer, playerHandObject);

        
        if (runner.IsServer)
        {
            Debug.Log("Is Server - " + player.PlayerId);
        }
        else
        {
            Debug.Log("Is not Server" + player.PlayerId);
        }
    }
    
    void INetworkRunnerCallbacks.OnPlayerLeft(NetworkRunner runner, PlayerRef player)
    {
        Debug.Log("Player left");
    }

    void INetworkRunnerCallbacks.OnReliableDataReceived(NetworkRunner runner, PlayerRef player, ArraySegment<byte> data)
    {
    }

    void INetworkRunnerCallbacks.OnSceneLoadDone(NetworkRunner runner)
    {
    }

    void INetworkRunnerCallbacks.OnSceneLoadStart(NetworkRunner runner)
    {
    }

    void INetworkRunnerCallbacks.OnSessionListUpdated(NetworkRunner runner, List<SessionInfo> sessionList)
    {
    }

    void INetworkRunnerCallbacks.OnShutdown(NetworkRunner runner, ShutdownReason shutdownReason)
    {
    }

    void INetworkRunnerCallbacks.OnUserSimulationMessage(NetworkRunner runner, SimulationMessagePtr message)
    {
    }
}
0

There are 0 best solutions below