0
\$\begingroup\$

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?

\$\endgroup\$
4
  • \$\begingroup\$ I don't understand a word but just from the error message, the file name and the class name seems to be different. Renaming either to match the other should fix the error \$\endgroup\$
    – Zibelas
    Mar 28 at 11:46
  • 2
    \$\begingroup\$ It seems you use a robot to help you write code for your game. Using robots to get help to write code is generally advised only when you know how to write code because then you can evaluate what the robot suggests, and you can debug the code yourself. In your situation, I suggest you find tutorial resources in Russian that would help you get going. Also, I would suggest you find some Russian based forums/chats that would help you get more "direct" help, without relying on translation tools that might hinder communication. \$\endgroup\$
    – Vaillancourt
    Mar 28 at 12:38
  • 1
    \$\begingroup\$ Once you get out of basic issues with code and Unity, there is a Russian StackOverflow that would let you post questions in Russian and get help in Russian, which would allow you to get over the language barrier. \$\endgroup\$
    – Vaillancourt
    Mar 28 at 12:40
  • 1
    \$\begingroup\$ FWIW, once you get enough reputation, there is a Russian chat infrastructure integrated to StackExchange. \$\endgroup\$
    – Vaillancourt
    Mar 28 at 12:43

2 Answers 2

1
\$\begingroup\$

You need to add using UnityEngine; to the very beginning of the script because unity does not know what a collider is. As the error suggests:

are you missing a using directive?

Also you should name the file same as the class, so TOUGT RESTART.cs should be renamed to RestartGameOnTouch.cs

You should also try to avoid spaces whenever you are naming things in unity because unity cannot always handle spaces in names.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Why do you re-state what has already been said in the other answer? \$\endgroup\$
    – Vaillancourt
    Mar 28 at 13:34
  • \$\begingroup\$ @Vaillancourt reputation, I'm assuming. \$\endgroup\$
    – Pow
    Mar 28 at 22:21
  • \$\begingroup\$ I'm since like 3 days on this page so I don't know everything about this right away \$\endgroup\$
    – CheckerT
    Mar 30 at 11:22
  • \$\begingroup\$ @CheckerT Okay, welcome to the site; typically, if something has already been mentioned, don't repeat it as folks reading the answers here don't want to re-read something that was already there. If you have new elements that are not covered, then you're welcome to post them. If you think something stated in another answer is not precise enough, then clearing things up is also appropriate, but it should add something new. \$\endgroup\$
    – Vaillancourt
    Mar 30 at 19:57
1
\$\begingroup\$

This is the compiler saying "I don't know what this Collider name is referring to".

Collider is a class in the UnityEngine namespace.

So you probably accidentally deleted (or forgot to add) the using UnityEngine; declaration at the top of the script (before public class RestartGameOnTouch).

As Zibelas points out in the comments, you should also generally name your file the same as the main type it defines. So a class called RestartGameOnTouch should be in a file named RestartGameOnTouch.cs not TOUGT RESTART.cs

There's also no overload of GetActiveScene() that takes an argument like 1, so your change to the restart method does not make sense. It's unclear what you were trying to do here.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .