Referencing instantiated scripts

Hello, I’m pretty inexperienced with coding and am trying to figure out how to reference a float value in an instantiated object. Essentially, I am trying to take the value of a ore (The instantiated object) and have a furnace read the value and give the value back as money. I plan on having the ore value be modifiable by different upgraders to increase the value so being able to read fluctuating values is what I’m working towards.

Here is the code for the furnace to get value from the ore

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

public class FurnaceSell : MonoBehaviour
{
    public OreValue oreValueScript;
    [SerializeField] GameObject ore;

    void Awake()
    {
        oreValueScript = ore.GetComponent<OreValue>();
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Ore"))
        {
            CurrencyManager.money += Mathf.RoundToInt(oreValueScript.oreValue);
            CurrencyManager.UpdateMoney();
            Destroy(other.gameObject);
        }
    }
}

and the code for the ore value (It is very simple as I’m just trying to get this working)

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

public class OreValue : MonoBehaviour
{
    public float oreValue = 5.0f;
}

As of right now the ore are worth 5, and if I edit a value in play mode it will give that corresponding value. The issue is that other ores that are said to be worth 5 are then processed as giving that editted value. Say I change the value to 10, then everything after that would be worth 10 despite saying its worth 5. I would like to, as said earlier, be able to reference each instantiated ore so that this problem can be prevented. Cheers.

Sounds great! How’s it going?

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
    - what actually happened, log output, variable values, and especially any errors you see
  • links to documentation you used to cross-check your work (CRITICAL!!!)

The general notes for what you are doing:

Referencing variables, fields, methods (anything non-static) in other script instances:

It isn’t always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.

Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.

That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.

If you’re just seeing a NullReferenceException, you NEVER have to post for those because the answer is ALWAYS the same… ALWAYS!

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
    
public class FurnaceSell : MonoBehaviour
{

    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Ore"))
        {
            float value=other.gameObject.GetComponent<OreValue>().oreValue;
            CurrencyManager.money += (int)value;
            CurrencyManager.UpdateMoney();
            Destroy(other.gameObject);
        }
    }
}
1 Like

This worked! Thank you very much, it was a lot simpler than I thought it’d be. The only issue I’m facing now is that the value is being doubled when processing it, but I believe I’ll be able to fix it.

I figure out the issue. I simply applied the script to two objects that were both getting value at the same time.