Script for restart level = Big BUG

Hello,
i do Junior Programmer:Create with Code 2- Unity Learn ,

to avoid restarting the game each time, I created a script to restart the scene :

using UnityEngine;
using UnityEngine.SceneManagement;
public class restartLevel : MonoBehaviour
{ public string SceneName ;
      Update()
    { if(Input.GetKeyDown(KeyCode.R))
        { SceneManager.LoadScene(SceneName);
}}}

Or :

using UnityEngine;
using UnityEngine.SceneManagement;
public class restartLevel : MonoBehaviour
{ Update()
    { if(Input.GetKeyDown(KeyCode.R))
        { SceneManager.LoadScene(0);
}}}

Or :

using UnityEngine;
using UnityEngine.SceneManagement;
public class restartLevel : MonoBehaviour
{ Update()
    { if(Input.GetKeyDown(KeyCode.R))
        { SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}}}

My problem :

The scene is playing, but my variables in my other scripts are going crazy, the player is jumping lower and lower with each replacement, my spawn objects are composing really weird.

Should I add a line of code? To reset my variables.
Is the script bad?

Here is the player controller code :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController3 : MonoBehaviour
{   private Rigidbody playerRb;
    private Animator playerAnim;
    private AudioSource playerAudio;
    public float forceJump;
    public float gravityModifier;
    public bool isOnGround = true;
    public bool gameOver = false;
    public ParticleSystem explosionParticle;
    public ParticleSystem dirtParticle;
    public AudioClip jumpSound;
    public AudioClip crashSound;
 
  void Start()
    {   playerRb = GetComponent<Rigidbody>();
        playerAnim = GetComponent<Animator>();
        playerAudio = GetComponent<AudioSource>();
        Physics.gravity *= gravityModifier;
    }

    void Update()
    {  if (Input.GetKeyDown(KeyCode.Space)&& isOnGround && !gameOver )
       {  playerRb.AddForce(Vector3.up * forceJump, ForceMode.Impulse);
        isOnGround = false;
    playerAnim.SetTrigger("Jump_trig");
    dirtParticle.Stop();
    playerAudio.PlayOneShot(jumpSound, 1.0f);
    }}

    private void OnCollisionEnter(Collision collision) {
           if(collision.gameObject.CompareTag("Ground")&& !gameOver)
{ isOnGround = true;
dirtParticle.Play();}
          else if(collision.gameObject.CompareTag("obstacle")&& !gameOver)
{ gameOver = true;
playerAnim.SetBool("Death_b", true);
playerAnim.SetInteger("DeathType_int", 1);
explosionParticle.Play();
dirtParticle.Stop();
playerAudio.PlayOneShot(crashSound, 1.0f);
}}}

unity 2021.3.2f1

If I understood correctly after many tests, the problem would come from : gravityModifier.
But I still don’t know why or how to fix it.

If that’s how your code is formatted… I suggest reformatting it to readable standards.

Nonetheless, unless you’re resetting the value of Physics.gravity, every time you restart (read, reload the scene) you are going to keep multiplying gravity from the value it was manipulated to during the last scene reload.

So if you aren’t, you need to remember to reset the value, as that won’t be reset when the scene is reloaded.

1 Like

formatted, convention ??
No, I don’t write my code like that, I modified it to save space, to avoid a Thread that was too long, which could discourage people from reading me and therefore helping me.

I understand the problem better indeed if my gravity is multiplied by my gravityModifier at each reboot :confused:

it works better like this ^^ :
Physics.gravity = new Vector3(0, -1.0F, 0);

rather :
Physics.gravity = new Vector3(0,-9.81F, 0);

I made a :
Debug.Log(Physics.gravity);
To find the value.

But you can find it in Project Settings → Physics → Cloth Gravity.

It might be useful for someone ^^

Thank you spiney199 ^^

Glad you could work this one out. Also…

You’ll discourage more people with hard to read code. Just a tip for the future.

1 Like

I understand, I would be more careful in the future, I understood the standards of the common convention, on the other hand when I do a unity learn tutorial, I keep the code as written in them, then once finished I’m rewriting all the code to my needs, in my personal script base.

I guess the biggest issue here is the placement of the braces and the sparse code. So I’ll be more careful ^^

Here I used the method for Physics.gravity, but if you know another way to do it I would be happy to know it ^^

I want to thank you because even if I managed to identify where the problem came from, I did not know that the global parameters would not restore it, I still have a lot to learn ^^