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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Scene Switching not working Correctly?

Discussion in 'Scripting' started by joesrelo, Apr 7, 2020.

  1. joesrelo

    joesrelo

    Joined:
    Mar 30, 2020
    Posts:
    4
    Hi,

    I'm attempting to make an Arena Shooter, and I have all the ground work developed and implemented. (Utilizing Proton). However, the stupidest issue is getting me snagged.

    I am trying to change the scene from the map to the title screen, however for some unknown reason it only works in game for either the first person to join a room or if the room only has one person.

    I've attached the code for the pause menu and the joining/creating room functionality, any help would be greatly appreciated.


    Pause Menu (Activated by a keypress in character, quit is triggered via a button on UI):
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine.SceneManagement;
    using Photon.Pun;
    using UnityEngine;
    public class Pause : MonoBehaviour
    {
    public static bool paused = false;
    public bool disconnecting = false;
    public void TogglePause()
    {
    if (disconnecting) return;
    paused = !paused;
    transform.GetChild(0).gameObject.SetActive(paused);
    Cursor.lockState = (paused) ? CursorLockMode.None : CursorLockMode.Confined;
    Cursor.visible = paused;
    }
    public void Quit()
    {
    disconnecting = true;
    PhotonNetwork.LeaveRoom();
    SceneManager.LoadScene(0);
    }
    }

    Starting/Joining Rooms (Relevant Functionality is bolded):
    public class Launcher : MonoBehaviourPunCallbacks
    {
    public InputField usernameField;
    public InputField roomnameField;
    public Slider maxPlayersSlider;
    public Text maxPlayersValue;
    public static ProfileData myProfile = new ProfileData();
    public GameObject tabMain;
    public GameObject tabRooms;
    public GameObject tabCreate;
    public GameObject buttonRoom;
    private List<RoomInfo> roomList;
    public bool isConnected;
    public Text connecting;
    public Text connected;
    public void Awake()
    {
    PhotonNetwork.AutomaticallySyncScene = true;
    myProfile = Data.LoadProfile();
    if (!string.IsNullOrEmpty(myProfile.username))
    {
    usernameField.text = myProfile.username;
    }
    Connect();
    connecting.enabled = true;
    connected.enabled = false;
    }

    public override void OnConnectedToMaster()
    {
    isConnected = true;
    connecting.enabled = false;
    connected.enabled = true;
    PhotonNetwork.JoinLobby();
    base.OnConnectedToMaster();
    }
    public override void OnJoinedRoom()
    {
    StartGame();
    base.OnJoinedRoom();
    }

    public override void OnJoinRandomFailed(short returnCode, string message)
    {
    Create();
    base.OnJoinRandomFailed(returnCode, message);
    }
    public void Connect()
    {
    PhotonNetwork.GameVersion = "0.0.0";
    PhotonNetwork.ConnectUsingSettings();
    }
    public void Join()
    {
    PhotonNetwork.JoinRandomRoom();
    }
    public void Create()
    {
    RoomOptions options = new RoomOptions();
    options.MaxPlayers = (byte) maxPlayersSlider.value;
    ExitGames.Client.Photon.Hashtable properties = new ExitGames.Client.Photon.Hashtable();
    properties.Add("map", 1);
    options.CustomRoomProperties = properties;
    PhotonNetwork.CreateRoom(roomnameField.text, options);
    }

    public void ChangeMap()
    {
    }
    public void ChangeMaxPlayerSlider (float t_value)
    {
    maxPlayersValue.text = Mathf.RoundToInt(t_value).ToString();
    }
    public void TabCloseAll()
    {
    tabMain.SetActive(false);
    tabRooms.SetActive(false);
    tabCreate.SetActive(false);
    }
    public void TabOpenMain()
    {
    TabCloseAll();
    tabMain.SetActive(true);
    }
    public void TabOpenRooms()
    {
    TabCloseAll();
    tabRooms.SetActive(true);
    }
    public void TabOpenCreate()
    {
    TabCloseAll();
    tabCreate.SetActive(true);
    }
    private void ClearRoomList()
    {
    Transform content = tabRooms.transform.Find("Scroll View/Viewport/Content");
    foreach (Transform a in content) Destroy(a.gameObject);
    }
    public override void OnRoomListUpdate(List<RoomInfo> p_list)
    {
    roomList = p_list;
    ClearRoomList();
    Transform content = tabRooms.transform.Find("Scroll View/Viewport/Content");
    foreach (RoomInfo a in roomList)
    {
    GameObject newRoomButton = Instantiate(buttonRoom, content) as GameObject;
    newRoomButton.transform.Find("Name").GetComponent<Text>().text = a.Name;
    newRoomButton.transform.Find("Number").GetComponent<Text>().text = a.PlayerCount + " / " + a.MaxPlayers;
    newRoomButton.GetComponent<Button>().onClick.AddListener(delegate { JoinRoom(newRoomButton.transform); });
    }
    base.OnRoomListUpdate(roomList);
    }
    public void JoinRoom (Transform p_button)
    {
    string t_roomName = p_button.transform.Find("Name").GetComponent<Text>().text;
    PhotonNetwork.JoinRoom(t_roomName);
    }

    public void StartGame()
    {
    if (string.IsNullOrEmpty(usernameField.text))
    {
    myProfile.username = "Guest " + Random.Range(100, 1000);
    }
    else
    {
    myProfile.username = usernameField.text;
    }
    if(PhotonNetwork.CurrentRoom.PlayerCount == 1)
    {
    Data.SaveProfile(myProfile);
    PhotonNetwork.LoadLevel(1);
    }
    }

    }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
  3. joesrelo

    joesrelo

    Joined:
    Mar 30, 2020
    Posts:
    4
    Oh, thanks. I haven't posted here before so that's a huge help!
     
    Joe-Censored likes this.