簡體   English   中英

如何使用 c# 在 Unity 中移動到下一個級別/場景

[英]How to move to next level/scene in Unity using c#

如果玩家 object 觸及另一個 object,我想制作一個從場景 Game2 移動到場景 Game4 的腳本。

我編寫了以下代碼來做到這一點:

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class NextLevel : MonoBehaviour
{
    [SerializeField]
    string strTag;

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.collider.tag == strTag)
            SceneManager.LoadScene(Game4);
    }
}

但是,當我使用它時,它會給出錯誤: Assets/Scripts/General/Collsion.cs(14,36): error CS0103: The name 'Game4' does not exist in the current context

這是一款 3d 游戲。

謝謝您的幫助!

你可能的意思是使用

SceneManager.LoadScene("Game4");

你的腳本中沒有任何東西叫做Game4並且LoadScene需要一個字符串,所以在我看來你只是缺少一些" ;)


順便說一句,而是使用CompareTag而不是== 第二個可能會因拼寫錯誤或不存在的標簽而默默失敗。 CompareTag會引發異常,並且速度也稍快一些。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM