HP and Credit

I’m trying to script so that when my character’s HP reaches 0 the Credit gets subtracted by 1.

I get the error
"
NullReferenceException: Object reference not set to an instance of an object
LevelManager+c__Iterator0.MoveNext () (at Assets/Scripts/SceneScripts/LevelManager.cs:50)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
UnityEngine.MonoBehaviour:StartCoroutine(String)
LevelManager:RespawnPlayer() (at Assets/Scripts/SceneScripts/LevelManager.cs:38)
playerHPManager:Update() (at Assets/Scripts/PlayerScripts/playerHPManager.cs:34)"

Credit: Manager code

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

public class CreditManager : MonoBehaviour {

    public int maxCredit;
    public static int PlayerCredit;

    public bool hasCredit;

    Text text;

    //private LevelManager levelManager;

    // Use this for initialization
    void Start()
    {
        text = GetComponent<Text>();
        PlayerCredit = maxCredit;
      //  levelManager = FindObjectOfType<LevelManager>();

        hasCredit = true;
    }

    // Update is called once per frame
    void Update()
    {
        if (PlayerCredit <= 0)
        {
            //PlayerHP = 0;
            //levelManager.RespawnPlayer();
            hasCredit = false;

        }
        text.text = "" + PlayerCredit;

    }

    public static void lessCredit(int creditToLose)
    {

       PlayerCredit -= creditToLose;
    }

  
}

Credit Less Code:

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

public class MinusCredit : MonoBehaviour {

    public int creditLess;
    public bool isDead;

    // Use this for initialization
    void Start () {
        creditLess = 1;
        isDead = false;
    }
  
    // Update is called once per frame
    void Update () {
      
    }

    public void lessWhenDead() {

        CreditManager.lessCredit(creditLess);
        isDead = true;
  
    }
}

the call from when HP gets to 0 from my HPManager

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

public class playerHPManager : MonoBehaviour {
  
    public int maxPlayerHP;
    public static int PlayerHP;
    public int StartingHP;

    public MinusCredit minuscredit;

    public bool isDead;

    Text text;

    private LevelManager levelManager;

    // Use this for initialization
    void Start () {
        text = GetComponent<Text>();
        PlayerHP = StartingHP;
        levelManager = FindObjectOfType<LevelManager>();
        minuscredit = FindObjectOfType<MinusCredit>();

        isDead = false;
    }
   
    // Update is called once per frame
    void Update () {
        if (PlayerHP <= 0) {
            PlayerHP = 0;
            levelManager.RespawnPlayer();
            isDead = true;

            minuscredit.lessWhenDead();
       
        }
        text.text = "" + PlayerHP;
       
    }

    public static void HurtPlayer(int damageToGive) {

        PlayerHP -= damageToGive;
    }

    public void FullHealth() {

        PlayerHP = maxPlayerHP;
       
    }
}

Something is null in the LevelManager.cs file at the 50th line. Whatever it is, you haven’t included in your post above.

Do you have a LevelManager and a MinusCredit in your scene?

Actually
That was an previous error
I reverted back a few steps

here’s the actual error

"
NullReferenceException: Object reference not set to an instance of an object
playerHPManager.Update () (at Assets/Scripts/PlayerScripts/playerHPManager.cs:37)"

Always start with the first when it comes to errors. Sometimes the first can be the cause for the rest. Not saying that’s the case here, but start there.

NullReferenceException: Object reference not set to an instance of an object
LevelManager+c__Iterator0.MoveNext () (at Assets/Scripts/SceneScripts/LevelManager.cs:50)

This is saying you’re missing an instance of an object in the “LevelManager” script on line 50. You haven’t shown use was that script is here, but that’s where you should start. Chances are it sounds like an inspector object was not placed or selected within a object reference. It has nothing to reference.

Ok. Thanks,I got it past the error. I attached the minus credit.cs to a game object.

but now It decreases the credit count straight to 0
So what I did was
When the HP in the Player HP manager.cs gets to 0 it will call the minus credit which in turn calls the function from the credit manager

call from HP manager

void Update () {
        if (PlayerHP <= 0) {
            PlayerHP = 0;
            levelManager.RespawnPlayer();
            isDead = true;

            minuscredit.lessWhenDead();
       
        }
        text.text = "" + PlayerHP;
       
    }

here’s the credit manager.cs

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

public class CreditManager : MonoBehaviour {

    public int maxCredit;
    public static int PlayerCredit;

    public bool hasCredit;

    Text text;

    //private LevelManager levelManager;

    // Use this for initialization
    void Start()
    {
        text = GetComponent<Text>();
        PlayerCredit = maxCredit;
      //  levelManager = FindObjectOfType<LevelManager>();

        hasCredit = true;
    }

    // Update is called once per frame
    void Update()
    {
        if (PlayerCredit <= 0)
        {
            //PlayerHP = 0;
            //levelManager.RespawnPlayer();
            PlayerCredit = 0;
            hasCredit = false;
            Debug.Log("No credit left");

        }
        text.text = "" + PlayerCredit;

    }

    public static void lessCredit(int creditToLose)
    {

       PlayerCredit -= creditToLose;
    }

   
}

and here is credit minus.cs I did set creditLess to 1 in the inspector.

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

public class MinusCredit : MonoBehaviour {

    public int creditLess;
    public bool isDead;
  

    // Use this for initialization
    void Start () {

        isDead = false;
    }
   
    // Update is called once per frame
    void Update () {
       
    }

    public void lessWhenDead() {
        isDead = true;
        if(isDead==true)
            CreditManager.lessCredit(creditLess);
        isDead = false;
   
    }
}

Looks like you’re jumping around a lot. You should try and limit that as much as possible if you can. It will make it less confusing and you wont have variables floating in multiple scripts especially ones that are similar like isDead.

That being said, it looks like you bounce back to the CreditManager script and call a function with 2 different references which aren’t playing out the way you want. Easiest way to fix this? Store a variable in only 1 of those files of what the deduction to credits will be, whether it’s a solid number or a percent, then simply deduct it when in the coinciding function.

Right now you’re trying to pass a int that isn’t accessible from one script to another. Or it’s not passing as it should.

Thanks for the help it’s working perfectly now

1 Like