Some problems with scripts

Hi,
I’m a beginner in Unity 5 and i’m trying to create a FPS
For the moment, there is only one mission, and there is a problem with the player health. If I die, health got reset to 100, but if I survive until the end and I restart just after, i will have the same amount of health than before. I already tried to reset health just before level start but it doesn’t work :cry:

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class SurviveMissionScript : MonoBehaviour {

    private Scene scene;
    public static int PlayerHealth = 100;

    void Start () {
        PlayerHealth = 100;
    }

    public void ChangeScene (string sceneName)
    {
        PlayerHealth = 100;
        SceneManager.LoadScene (1);
    }

}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class GlobalHealth : MonoBehaviour {

    public static int PlayerHealth = 100;
    public int InternalHealth;
    public GameObject HealthDisplay;

    void Update() {
        InternalHealth = PlayerHealth;
        HealthDisplay.GetComponent<Text> ().text = "Health: " + PlayerHealth;
      if (PlayerHealth == 0) {
            Cursor.visible = (true);
            SceneManager.LoadScene (2);
            PlayerHealth = 100;
    }
    }
}

There is also an ammo system, with a limited number of ammunition. However, if the Total Ammo is null, i want the reloading to be disabled. But it keeps reloading and decreases the Total Ammo…

Here’s the script for the SMG

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class SMG : MonoBehaviour {

    public float damage = 10f;
    public float range = 100f;
    public float fireRate = 15f;
    public float impactForce = 30f;

    public AudioSource SMGShot;
    public AudioSource ReloadingSound1;

    public int maxAmmo = 25;
    public int TotalAmmo = 50;
    private int currentAmmo;
    public float reloadTime = 1f;
    private bool isReloading = false;
    public GameObject AmmoDisplay;

    public Camera fpsCam;
    public ParticleSystem muzzleFlash;
    public GameObject impactEffect;

    private float nextTimeToFire = 0f;

    public Animator animator;

    void Start ()
    {
        currentAmmo = maxAmmo;
    }

    void OnEnable()
    {
        isReloading = false;
        animator.SetBool ("Reloading", false);
    }

    void Update () {

        AmmoDisplay.GetComponent<Text> ().text = "Ammo: " + currentAmmo + "/" + TotalAmmo;

        if (isReloading)
            return;

        if (currentAmmo <= 0)
        {
            StartCoroutine (Reload());
            return;
        }

        if (TotalAmmo <= 0 && currentAmmo <= 0)
        {
            isReloading = false;
        }

        if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
        {
            nextTimeToFire = Time.time + 1f / fireRate;
            Shoot ();
            SMGShot.Play();
        }
   
    }


    IEnumerator Reload ()
    {
        isReloading = true;
        Debug.Log ("Reloading...");

        animator.SetBool ("Reloading", true);
        ReloadingSound1.Play ();

        yield return new WaitForSeconds (reloadTime - .25f);
        animator.SetBool ("Reloading", false);
        yield return new WaitForSeconds (.25f);

        currentAmmo = maxAmmo;
        TotalAmmo -= 25;
        isReloading = false;
    }

    void Shoot ()
    {
        muzzleFlash.Play ();

        currentAmmo--;

        RaycastHit hit;
        if (Physics.Raycast (fpsCam.transform.position, fpsCam.transform.forward, out hit))
        {
            Debug.Log (hit.transform.name);

            Target target = hit.transform.GetComponent<Target>();
            if (target != null)
            {
                target.TakeDamage(damage);
            }

            if (hit.rigidbody != null)
            {
                hit.rigidbody.AddForce (-hit.normal * impactForce);
            }

            GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal)) as GameObject;
            Destroy(impactGO, 2f);
        }
    }
}

Third problem, when i shoot too much below, it will touch the FPS controller (the one of the standard assets) instead of the ground

And fourth, even if it is not really a problem, I would like to know, what is for you, the best ay to keep a save of the progress in a game. I want to create a leveling up system, and weapons unlocking system… and I don’t know how and where to start ^^

Thanks for your help

You should use PlayerPrefs. You can load them while restarting, you can save locally using PlayerPrefs. If you want more help, or want me to script it for you, reach me on Fiverr. ( Desktop Applications Development Services Online | Fiverr )

By you not naming the scene, I guess (1) is your menu and (2) is Gameplay scene.

  1. Move PlayerHealth = 100; before Load scene. Just in case, you maybe don’t know, PlayerHealth in two script is not the same one.
  Cursor.visible = (true);
PlayerHealth = 100; // <-- Move to here
SceneManager.LoadScene (2);
//PlayerHealth = 100;
  1. You should check if the totalAmmo > 0 before call Reload.
if (currentAmmo <= 0)
{
    // Should check TotalAmmo before start Reload
    if (TotalAmmo <= 0)
    {
        return;
    }
    else
    {
        StartCoroutine (Reload());
    }
     return;
 }
// It make no sense here, remove it
if (TotalAmmo <= 0 && currentAmmo <= 0)
{
    isReloading = false; 
}
  1. Very unclear and without code, I can’t find out the problem.

  2. Easy and fast: PlayerPrefs. Another solution is to save as XML files.

Bonus: to be honest, your code was bad. If you are new to Unity and try to learn and use it in feature development, please take time to learn basic and make the small game first.

Thanks, I didn’t know that was a thing ^^ I will try to use it as soon as i can

Thanks for your answer ^^ I will try to change it as fast as i can!
In which way is it bad? I mainly used tutoriels on youtube so i don’t really know their quality. But you’re right, i need to start from the beginning :wink:

And sorry for not saying it but the 0 scene is the menu, the 1 is the gameplay scene and the 2 is the Game Over scene