Home > database >  How to fix "Cannot implicitly convert type 'void' to 'UnityEngine.AsyncOperation
How to fix "Cannot implicitly convert type 'void' to 'UnityEngine.AsyncOperation

Time:01-28

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'

CodePudding user response:

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()

  •  Tags:  
  • Related