Spawn point for specific player

Options
Hi! i used this code to spawn my specific player at specific spawn point.when player leave room i want to able to spawn new player at spawn point that was available by player that left, and i used PlayerNumbering but it didn't work ,it shows that my number of localplayer is 0 ,but actually it's 1 in switchchat class.It makes spawn each player at one same spawn point. Do you have any suggestions or solutions&
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Realtime;
using Photon.Pun;
using ExitGames.Client.Photon;
using UnityEngine.SceneManagement;
using Photon.Pun.UtilityScripts;

public class switchchar : MonoBehaviourPunCallbacks {
    [SerializeField] GameObject[] SpawnPoints;
    [SerializeField] GameObject blacktran;
    [Space(20f)]
  

   
    public GameObject default_gg;
    [SerializeField] private GameObject camouflage_gg;
    [SerializeField] private GameObject angel_gg;
    [SerializeField] private GameObject tiger_gg;
    [SerializeField] private GameObject devil_gg;
    public static GameObject pl;
    
    
    public static int localPlayerIndex;
    private bool isLocalPlayerIndexSet { get { return localPlayerIndex >= 0; } }
    

    public override void OnEnable() {
        base.OnEnable();
        PlayerNumbering.OnPlayerNumberingChanged += OnPlayerNumberingChanged;
    
    } 
    public override void OnDisable() {
        base.OnDisable();
        PlayerNumbering.OnPlayerNumberingChanged -= OnPlayerNumberingChanged;
    }

    private IEnumerator Start() {
        while (!isLocalPlayerIndexSet) {
            yield return null;
        }
        Debug.Log(switchchar.localPlayerIndex + " local");
        NetworkInstantiation();
    }

    private void OnPlayerNumberingChanged() {
        if (!isLocalPlayerIndexSet) {
            localPlayerIndex = PhotonNetwork.LocalPlayer.GetPlayerNumber();
           
        }
        
    }
    

    void NetworkInstantiation() {
       
        
        if (isLocalPlayerIndexSet) {
            if (imagespr.ine == 0) 
                pl = MasterManager.NetworkingInstantiate(default_gg, SpawnPoints[localPlayerIndex].transform.position, Quaternion.identity);
                pl.transform.SetSiblingIndex(1);
        }
    }


      
    }

    

}

Comments

  • JohnTube
    JohnTube ✭✭✭✭✭
    edited December 2020
    Options
    Hi @Zeynolla,
    when player leave room i want to able to spawn new player at spawn point that was available by player that left

    Not sure if player index is automatically recycled on leave.
    I need to test this.
    I was working on a different version for player numbering/indexing, maybe I can finish it soon and you can test it to see if it suits your needs.

    Until then, did you try resetting localPlayerIndex on player leave?
    Also set default localPlayerIndex value to -1, since first index is 0.
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Photon.Realtime;
    using Photon.Pun;
    using ExitGames.Client.Photon;
    using UnityEngine.SceneManagement;
    using Photon.Pun.UtilityScripts;
    
    public class switchchar : MonoBehaviourPunCallbacks {
        [SerializeField] GameObject[] SpawnPoints;
        [SerializeField] GameObject blacktran;
        [Space(20f)]
      
    
       
        public GameObject default_gg;
        [SerializeField] private GameObject camouflage_gg;
        [SerializeField] private GameObject angel_gg;
        [SerializeField] private GameObject tiger_gg;
        [SerializeField] private GameObject devil_gg;
        public static GameObject pl;
        
        
        public static int localPlayerIndex = -1;
        private bool isLocalPlayerIndexSet { get { return localPlayerIndex >= 0; } }
        
    
        public override void OnEnable() {
            base.OnEnable();
            PlayerNumbering.OnPlayerNumberingChanged += OnPlayerNumberingChanged;
        
        } 
        public override void OnDisable() {
            base.OnDisable();
            PlayerNumbering.OnPlayerNumberingChanged -= OnPlayerNumberingChanged;
        }
    
        private IEnumerator Start() {
            while (!isLocalPlayerIndexSet) {
                yield return null;
            }
            Debug.Log(switchchar.localPlayerIndex + " local");
            NetworkInstantiation();
        }
    
        
    public void override OnLeftRoom(){
        localPlayerIndex = -1;
    }
    
    
        private void OnPlayerNumberingChanged() {
            if (!isLocalPlayerIndexSet) {
                localPlayerIndex = PhotonNetwork.LocalPlayer.GetPlayerNumber();
               
            }
            
        }
        
    
        void NetworkInstantiation() {
            if (isLocalPlayerIndexSet) {
                if (imagespr.ine == 0) 
                    pl = MasterManager.NetworkingInstantiate(default_gg, SpawnPoints[localPlayerIndex].transform.position, Quaternion.identity);
                    pl.transform.SetSiblingIndex(1);
            }
        } 
        }
    }
    
  • Zeynolla
    Options
    Hi!@JohnTube mod, Could you inform me when you finish it?
    And yes i set but it anyway spawn my both player at spawn point #0 at starting joining to room
    pl = MasterManager.NetworkingInstantiate(default_gg, SpawnPoints[localPlayerIndex].transform.position, Quaternion.identity);
    
    localPlayerIndex in this script("switchchar") is 0 in any case at both players.
    Thank you for respond!And Happy coming New Year !
  • JohnTube
    JohnTube ✭✭✭✭✭
    Options
    Could you inform me when you finish it?
    No ETA and may not be released at all.

    Make sure both players are joined to the same room.
    You can see the full list of indexes in the custom inspector of PlayerNumbering script in the Unity Editor.
    You can clearly see the index + actor number + custom properties there to debug this.