[英]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.