Cassie
Cassie

Reputation: 3099

Redirect from one scene to another Unity 3D script

I have 2 scenes and I want to redirect from one to another on the button click. I used AssetBundle for that. Here is the code for exporting of assets:

public class ExportAssetBundles
{
    [MenuItem("Assets/Build AssetBundle")]
    static void ExportResource()
    {
        string folderName = "AssetBundles";
        string filePath = Path.Combine(Application.streamingAssetsPath, folderName);

        BuildPipeline.BuildAssetBundles(filePath, BuildAssetBundleOptions.None, BuildTarget.NoTarget);
    }
}

And here is the code for loading:

public class RedirectToMenu : MonoBehaviour {
    void Start () {
        Button btn = GetComponent<Button>);
        btn.onClick.AddListener(OnClick);
    }

    public void OnClick(){
        Debug.Log("You have clicked the button!");
        LoadAsset("MenuFinal");
    }

    IEnumerator LoadAsset(string assetBundleName)//, string objectNameToLoad)
    {
        string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "AssetBundles");
        filePath = System.IO.Path.Combine(filePath, assetBundleName);

        var assetBundleCreateRequest = AssetBundle.LoadFromFileAsync(filePath);
        yield return assetBundleCreateRequest;

        AssetBundle asseBundle = assetBundleCreateRequest.assetBundle;
    }
}

So when I run my scene in Unity I get such error in console:

ArgumentException: The output path "D:/userdata/Documents/Scene1/Assets/StreamingAssets\AssetBundles" doesn't exist(at ExportAssetBundles.ExportResource () (at Assets/ExportAssetBundles.cs:16)

I have put my scenes into Assets folder:

enter image description here

Also, I have added them to Build settings:

enter image description here

So how can I fix this error and make the redirection work? Is that a code problem or some file locations problem?

Upvotes: 0

Views: 1040

Answers (2)

BenBenMushi
BenBenMushi

Reputation: 225

The path you ask for is

"D:/userdata/Documents/Scene1/Assets/StreamingAssets\AssetBundles"

but your real path is

"D:/userdata/Documents/Scene1/Assets/StreamingAssets\AssetBundle" <- no 's'

Upvotes: 1

David
David

Reputation: 16277

Use SceneManager.LoadScene()

public static void LoadScene(int sceneBuildIndex, 
                   SceneManagement.LoadSceneMode mode = LoadSceneMode.Single);
public static void LoadScene(string sceneName, 
                   SceneManagement.LoadSceneMode mode = LoadSceneMode.Single);

Upvotes: 2

Related Questions