Reset Object Position

I have figured out how to reset the scene for my game. I started telling people about it, and they all asked the same thing I never expected: SCORING. I have an idea for a score count and how to script that, but what I don’t know is how to reset all else but NOT the score. I eventually decided to set all goal detectors back to true (they are disabled when the ball enters the goal) and the positions of the 2 players and the ball back to their start positions. How exactly would I code that? This is what I have for the boundaries:

        private void Update()
        {
            if (EndGame == true)
            {
                if (Input.GetKey("r"))
                {
                   
                }
            }
       }

What would I input into this?

Hi, I see 2 possibilities :

  • either you save your score to PlayerPrefs, then reload your scene, then load the score you have stored in PlayerPrefs
  • or you store “manually” all your objects initial values (positions, rotations etc), in variables, at Start() ; then, on keyboard input, you call a method reseting all these values. This can be quite boring and source of possible errors if you have a lot of GameObjects in your game.

A few advices :

  1. I assume EndGame is a boolean. By convention it is preferable to use a lower case first character for variables : i.e : endGame. Beginning with an upper case character should be kept for methods, routines etc : i.e. DoSomething(bool myBoolean){}. Thus anyone reading your script will easily know the type your word represents.
  2. You can write if(endGame), it’s the same as if(endGame==true). And you can use the ! character for negation : if(!endGame) means the same as if(endGame==false).
  3. You’d better use if(input.GetKeyDown(Keycode.R)) than GetKey followed by a string.