我正在为Unity中的游戏编写死亡脚本。我在关卡下制作了一个没有纹理的3d盒子,并制作了对撞机isTrigger = true
。现在,我向该框中添加了一个脚本,当玩家输入触发器时,该脚本可重新加载当前场景。它的两行代码,我不知道为什么,但是出现错误:
Assets\scripts\death.cs(20,32): error CS0103: The name 'currentScene' does not exist in the current context
代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class death : MonoBehaviour
{
void Start()
{
Scene currentScene = SceneManager.GetActiveScene();
}
private void OnTriggerEnter(Collider other)
{
SceneManager.LoadScene(currentScene.buildIndex);
}
}
答案 0 :(得分:0)
我知道上面的评论已经注意到您的问题是局部变量,这要归功于它们,但这只是为了优化您的代码和内存。您可以仅保留OnTriggerEnter并删除开始。
private void OnTriggerEnter(Collider other)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
如果我们不再使用它,则无需将场景存储在变量中。只会浪费内存(坏习惯)