1

I've tried adapting the usual single-player loading bar code to one that'll work for multiplayer using PHOTON. Below is the code I've tried.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Photon.Pun;
using Photon.Realtime;
using UnityEngine.SceneManagement;

public class Launcher : MonoBehaviourPunCallbacks
{
    [SerializeField]
    public Button CreateNameButton;
    
    [SerializeField]
    private Slider LoadingSlider;

    void Awake()
    {
     PhotonNetwork.AutomaticallySyncScene = true;
    }

    public void loadLevels(int sceneIndex)
    {
        StartCoroutine(loadSelectedScene(sceneIndex));
    }

    IEnumerator loadSelectedScene(int sceneIndex)
    {
        
        AsyncOperation async = PhotonNetwork.LoadLevel(sceneIndex);

        while (!async.isDone){
            float progress = Mathf.Clamp01(async.progress / .9f);
            LoadingSlider.value = async.progress;
            yield return null;

       }
     }
}

I received the error:

Cannot implicitly convert type 'void' to 'UnityEngine.AsyncOperation'

Nationz
  • 13
  • 2
  • 2
    [`PhotonNetwork.LoadLevel(int levelNumber)`](https://doc-api.photonengine.com/en/pun/v2/class_photon_1_1_pun_1_1_photon_network.html#aedba3c40295e3684fedc64acf678a5cd) is a `void` method so you can't assign it to a `AsyncOperation` type. Additionally, you shouldn't use reserved words (e.g. `async`) as _variable names_. –  Jan 27 '22 at 06:26
  • @MickyD Yes, the method is a void method whereas the non-photon method isn't. I'm unsure as to how to edit this to allow me to track the loading time of the scene. – Nationz Jan 27 '22 at 06:36
  • _"Yes, the method is a void method whereas the non-photon method isn't"_ - I don't follow you. `AsyncOperation async = PhotonNetwork.LoadLevel(sceneIndex);` is invalid. If you wish to use the synchronous method then use `PhotonNetwork.LoadLevel(sceneIndex);` or for the _asynchronous_ version see the answer below. –  Jan 27 '22 at 06:39
  • I have reworded your question's title to better serve the essence of your problem. This will also help others now and in the future with similar issues. –  Jan 27 '22 at 07:19
  • Public fields are a poor choice and can lead to unpredictable state. Make then private especially when they are SerialiseField and back them up with public properties. Refer to the Unite talk featuring Shipbreakers for other best practices. –  Jan 27 '22 at 08:22
  • Interestingly enough public fields are required in Unity when the object is referenced in other scripts. Refer to Unity Basics to begin learning! – Nationz Jan 27 '22 at 08:37
  • You’re confusing properties with fields. C# 101 –  Jan 27 '22 at 08:40

1 Answers1

1

You can not assign PhotonNetwork.LoadLevel to AsyncOperation. So, you need to use

AsyncOperation asyncOperation = PhotonNetwork.LoadLevelAsync(sceneIndex);

However, in PUN 2 Unity, the LoadLevelAsync have been removed so you can use PhotonNetwork.LoadLevel()

I_Al-thamary
  • 3,385
  • 2
  • 24
  • 37
  • Hey thanks for the answer, I actually originally tried exactly this and I received this error: 'PhotonNetwork' does not contain a definition for 'LoadLevelAsync'. I googled this and someone mentioned using LoadLevel instead. Any ideas why this is the case? – Nationz Jan 27 '22 at 06:42
  • 1
    You can see the example in the answer and to solve this issue make sure, that you at least use PUN version 1.90 or a newer one. – I_Al-thamary Jan 27 '22 at 06:47
  • 1
    @Nationz not wanting to sound naggy but please take note of _["Looks like is solved isn't it? As for , that would be a different question, consider posting it separately."](https://meta.stackoverflow.com/a/253829/585968)_. After all, you did not mention _"I actually originally tried exactly this"_ in your question –  Jan 27 '22 at 06:47
  • I am using PUN 2 version 2.4 and It is still giving me this error. – Nationz Jan 27 '22 at 06:57
  • @Nationz don't take it personally. I provided you with helpful comments beneath your question and pointed out the simple syntax error in your code. Though if I could down-vote you again for your impertinence I would –  Jan 27 '22 at 07:10
  • @Nationz _["Editing a question so that the provided answers are invalid is not the right way for the original user to get assistance. If they have further questions, they should post a new question, not invalidate the help they've already been given."](https://meta.stackoverflow.com/a/351629/585968)_ –  Jan 27 '22 at 07:13
  • @Nationz _"...let someone who knows what `they're talking about` .."_ - [tell me again](https://mickyd.wordpress.com/2021/10/17/flight-sim-project-update/) –  Jan 27 '22 at 07:15
  • @Nationz Please see [LoadLevel or LoadLevelAsync](https://forum.photonengine.com/discussion/12055/loadlevel-or-loadlevelasync) – I_Al-thamary Jan 27 '22 at 07:31
  • @Nationz, I think that he updated more than that and both of you need to take it easy and it does not worth it. – I_Al-thamary Jan 27 '22 at 07:49
  • @I_Al-thamary I've read the documentation you posted and I've tried that solution but again it still states that Photon.Network doesn't have LoadLevelAsync(). I'm using Unity 2020.3.26f1 and PUN 2 version 2.4 – Nationz Jan 27 '22 at 07:56
  • @I_Al-thamary In the documentation I read LoadLevel() loads asynchronously https://forum.photonengine.com/discussion/12672/photonnetwork-loadlevelasync-removed-in-pun-2 – Nationz Jan 27 '22 at 07:59
  • Post it as a new question. Not only can you fine tune your post but you can get more reputation that way. –  Jan 27 '22 at 08:11
  • Rewording your question does help. A well constructed and informative title helps to draw in interest for todays readers as well as serving future readers with similar issues using search engines to find answers. –  Jan 27 '22 at 08:17
  • @MickD The title was accurate and informative. You changed it because you didn't understand it. The title was Orignally "How to create a loading bar in Unity using Photon 2.4". It's very basic and anyone experienced with Photon would be able to see the code and know the issue. – Nationz Jan 27 '22 at 08:25
  • @MickyD in the doccumentation I read the developer removed LoadLevelAsync() and LoadLevel() was changed to asynchronous. Which is why the error was confusing. I was looking for someone experienced in PUN to explain to me what the solution might be. Not what the error means. – Nationz Jan 27 '22 at 08:27
  • 1
    @Nationz though rewriting code to make an error go away gets you over a hurdle, the chance to learn has been missed. –  Jan 27 '22 at 08:40