0

I need help, i'am trying to add float number from a script to another script but it does not work. I'am new to c# and coding in general if somebody code fix the script and explained whats wrong with this i would be very thankful. This is the error i am getting. "NullReferenceException: Object reference not set to an instance of an object Resources.Die () (at Assets/Resources.cs:42) Resources.Update () (at Assets/Resources.cs:22)" Here is my scripts: 1st

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

public class Resources : MonoBehaviour {

    public float maxHealth = 5;
    public float currentHealth;
    public float ResourceCounter;

    public Texture stoneIcon;
    public Texture woodIcon;

    void Start ()
    {
        currentHealth = maxHealth;
        ResourceCounter = 0;
    }

    void Update ()
    {
        Die();

    }

    public void OnMouseOver()
    {
        if(Input.GetButtonDown("Fire1"))
        {
            Debug.Log("Loosing one health");
            currentHealth = currentHealth - 1f;
        }

    }

    public void Die()
    {
        if(currentHealth <= 0)
        {
            Destroy(gameObject);

            Inventory inventory = GetComponent<Inventory>();
            inventory.ResourceStone = inventory.ResourceStone + 1;

        }
    }
}

2nd

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

public class Inventory : MonoBehaviour {

    public float ResourceStone;

    // Use this for initialization
    void Start ()
    {
        ResourceStone = 0;
}

    // Update is called once per frame
    void Update () {

    }
}
Jarek Kożdoń
  • 336
  • 2
  • 13
  • Object reference not set to an instance of an object Resources.Die() this informs you that you're trying to access a variable without a value. it informs you that it's in line 42, but i don't know which exactly it is, i just assume that Inventory inventory = GetComponent(); returns nothing and next line is throwing null reference when you're trying to access inventory variable's property. try checking there if inventory != null – Jarek Kożdoń Apr 21 '18 at 23:16
  • BTW: by scripts you mean classes, right? – Jarek Kożdoń Apr 21 '18 at 23:16

1 Answers1

1

By looking at your code i think you never defined the inventory instance for your "resources" script. If you are using a getcomponent both the resources and the inventory script must be on the same gameobject to be found. If what you want requires both scripts to be on other gameobjects you need a reference to the inventory in your resources script. You can do this several way (like making a static reference and refering to it or defining inventory as a public variable and then adding the inventory reference in the unity editor)

This is how the first case would look:

First Script -

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

public class Inventory : MonoBehaviour
{
    public static Inventory instance;

    private void Awake()
    {
        instance = this;
    }

    public float ResourceStone;

// Use this for initialization
    void Start()
    {
        ResourceStone = 0;
    }
}

Second Script -

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

public class Resources : MonoBehaviour
{

    public float maxHealth = 5;
    public float currentHealth;
    public float ResourceCounter;

    public Texture stoneIcon;
    public Texture woodIcon;

    void Start()
    {
        currentHealth = maxHealth;
        ResourceCounter = 0;
    }

    void Update()
    {
        Die();
    }

    public void OnMouseOver()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            Debug.Log("Loosing one health");
            currentHealth = currentHealth - 1f;
        }

    }

    public void Die()
    {
        if (currentHealth <= 0)
        {
            Destroy(gameObject);
            Inventory.instance.ResourceStone++;
        }
    }
}

This is how the code would look if you did the second one:

first script:

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

public class Inventory : MonoBehaviour
{

public float ResourceStone;

// Use this for initialization
void Start()
{
    ResourceStone = 0;
}

// Update is called once per frame
    void Update()
    {

    }
}

second script-

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

public class Resources : MonoBehaviour
{

    public float maxHealth = 5;
    public float currentHealth;
    public float ResourceCounter;
    public Inventory inventory;

    public Texture stoneIcon;
    public Texture woodIcon;

    void Start()
    {
        currentHealth = maxHealth;
        ResourceCounter = 0;
    }

    void Update()
    {
        Die();

    }

    public void OnMouseOver()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            Debug.Log("Loosing one health");
            currentHealth = currentHealth - 1f;
        }

    }

    public void Die()
    {
        if (currentHealth <= 0)
        {
            Destroy(gameObject);
            inventory.ResourceStone++;
        }
    }
}

Just take in account two things to decide if you will use the first or the second one. The first one is a static reference, that means that you can't make more that one inventory. In the second one you need to manually drag the reference in the editor. Just in case you don't know, writing ++ after an integer does the same as integer = integer + 1;

EDIT: There, that's better looking :D

  • Will it work to use [RequireComponent(typeof(Inventory))] or public class Resources : Inventory ? –  Apr 21 '18 at 23:58
  • I'm having problems writing the code on here, but basically what i showed, will try to edit it on my Home PC back home. –  Apr 22 '18 at 00:02
  • Thank you so much for your help, i really appreciate it! –  Apr 22 '18 at 00:04
  • Glad to help, this is what this community is for :). –  Apr 22 '18 at 02:31