I asked Mail.RU how to make a script so that when an object is touched, the game will restart. They sent me this:
using UnityEngine.SceneManagement;
public class RestartGameOnTouch : MonoBehaviour
{
// Attach this script to an object that
// should restart the game when touched.
void OnTriggerEnter(Collider other)
{
// Check if the player has touched
// the object with this script.
if (other.CompareTag("Player"))
{
// Restart the game.
RestartGame();
}
}
void RestartGame()
{
// Here you can add any logic that needs
// to be executed before restarting the
// game, for example, resetting counters,
// player position, etc.
// For simplicity, we'll reload the scene:
var scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.buildIndex);
}
}
Then, using other answers from Mail.RU, I changed the restart method to:
void RestartGame()
{
var scene = SceneManager.GetActiveScene(1);
SceneManager.LoadScene(scene.buildIndex);
}
I don’t know what the problem is, but I get this error message:
Assets\Scenes\TOUGT RESTART.cs(5,25): error CS0246: The type or namespace name ‘Collider’ could not be found (are you missing a using directive or an assembly reference?)
When I try to run my game, it says that first I need to fix all compiler errors.
What do I need to do?