Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Sanity meter script help

Discussion in 'Scripting' started by SgtBossGamer12, Oct 2, 2014.

  1. SgtBossGamer12

    SgtBossGamer12

    Joined:
    Oct 2, 2014
    Posts:
    16
    Hello to everyone,
    I need help with a script I'm writing(I'm sort of a newb with javascript in unity.)I've made several text objects in my game for huds. What I want my script to do is to have the first hud on the screen, and when the player enters the box collider of the empty game object to trigger the jumpscare, the next hud pops up, and the sanity variable loses 20, and this repeats until the player has trigger 5 jumpscares. After the fifth one, a 6th and final hud pops up saying you lost your sanity, and take you back to the main menu.
    I've fixxed all of the error messages, but the other huds never appear when I enter the box collider.
    here is the script...

    var hud2 : GameObject;
    var hud3 : GameObject;
    var hud4 : GameObject;
    var hud5 : GameObject;
    var hud6 : GameObject;

    var hasplayed = false;
    var entertrigger = false;

    var Sanity = 100;


    function Start () {

    entertrigger = false;
    hasplayed = false;
    hud2.renderer.enabled = false;
    hud3.renderer.enabled = false;
    hud4.renderer.enabled = false;
    hud5.renderer.enabled = false;
    hud6.renderer.enabled = false;

    }

    function OnEnterTrigger (BoxCollider : CapsuleCollider) {
    entertrigger = true;

    }


    function Update () {

    if (entertrigger == true) {
    var Sanity1 = Sanity - 20;
    hud2.renderer.enabled = true;
    }
    if (Sanity == 80) {
    if (entertrigger == true) {
    var Sanity2 = Sanity - 40;
    hud3.renderer.enabled = true;
    }
    }
    if (Sanity == 60) {
    if (entertrigger == true) {
    var Sanity3 = Sanity - 60;
    hud4.renderer.enabled = true;
    }
    }
    if (Sanity == 40) {
    if (entertrigger == true) {
    var Sanity4 = Sanity - 80;
    hud5.renderer.enabled = true;
    }
    }
    if (Sanity == 20) {
    if (entertrigger == true) {
    var Sanity5 = Sanity - 100;
    hud6.renderer.enabled = true;
    }
    }
    }
    [/code]
     
  2. SgtBossGamer12

    SgtBossGamer12

    Joined:
    Oct 2, 2014
    Posts:
    16
    By the way, I haven't put the part that takes you to the main menu. Couldn't remember how to do that.
     
  3. jonSG

    jonSG

    Joined:
    Mar 26, 2014
    Posts:
    18
    you need to decrement sanity itself rather than create new "sanity" variables
     
  4. SgtBossGamer12

    SgtBossGamer12

    Joined:
    Oct 2, 2014
    Posts:
    16
    I'm having trouble doing that. Please help
     
  5. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    First of all, take a look at this sticky (Code Tags).

    As already mentioned by others, you always create local 'Sanity#' variables instead of working with your 'Sanity' variable.
    Your variable and thus the value you want to alter stays untouched.

    Change all 'var Sanity#' in your if-statements to 'Sanity' and only subtract 20 from it.
    You'd also need to set 'entertrigger' in every if statement to false again, as you would otherwise immediately run into the next if-statement.
    The trigger function is named 'OnTriggerEnter', not 'OnEnterTrigger'.
    Avoid namings like 'BoxCollider : CapsuleCollider', change it to something like 'trigger : Collider'.

    To be honest, you might have a look into some tutorials first as the code will not be efficient at all. It's better to start now as long as you're still new, otherwise it will be difficult to learn proper coding.
     
  6. SgtBossGamer12

    SgtBossGamer12

    Joined:
    Oct 2, 2014
    Posts:
    16
    Thanks for the help Suddoha and JonSG