Mining script

I am figuring out a mining script so that player can mine rocks and what not. It somewhat works but not as I would like it to. Basically it works for the first rock but any other rocks i place are not working at all. For example if i place just 1 rock it works fine. If i add 3 more rocks only the 4th one works and the first three don’t.

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

public class MiningRocks : MonoBehaviour
{

    public int distancetomine = 2;
    bool rockInRange;
    GameObject rock;
    IronOre ironore;


    // Use this for initialization
    void Awake()
    {
       rock = GameObject.FindGameObjectWithTag("IronOre");
       ironore = rock.GetComponent<IronOre>();

    }


    // Update is called once per frame
    void Update()
    {
        float distance = Vector3.Distance(this.transform.position, GameObject.FindGameObjectWithTag("IronOre").transform.position);
        if (Input.GetKeyDown(KeyCode.E) && distance <= distancetomine && rockInRange == true)
        {
            ironore.IronOreHealth -= 1;
        }
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject == rock)
        {
            rockInRange = true;
        }
    }

    void OnTriggerExit(Collider other)
    {
        if (other.gameObject == rock)
        {
            rockInRange = false;
        }
    }

    void OnGUI()
   {
            float distance = Vector3.Distance(this.transform.position, GameObject.FindGameObjectWithTag("IronOre").transform.position); ;
            if (distance <= 2)
            {
                GUI.Box(new Rect(400, 200, 220, 22), "Press E to gather");
            }
    }

}

Script is attached to the player. I would appreciate any help on what i am doing wrong.

PS (if i place 3 rocks) the GUI only shows for the 3rd rock placed. If i mine that rock it then shows to for 2nd and then for the first while it should show for all at all times + if i try to mine 1st or 2nd rock it doesnt work. It only works if i go 3rd>2nd>1st.

Just a guess, but perhaps the issue arises because you are using GetKeyDown. From it’s docs:“…since the state gets reset each frame. It will not return true until the user has released the key and pressed it”

As opposed to GetKey, which will always return true when the key is held down.

On awake, you call FindGameObjectWithTag(“IronOre”), which gives you the first rock that monobehavior finds with that tag.

Then, when finding the distance, you call the function again in calling the distance, in Update() and in onGUI.

Since FindGameObjecTWithTag will only return the first object found, it is no surprise that you’re getting the distance to the third rock.

When you call OnTriggerEnter, you check if the game object is that rock, i.e the first one that monobehavior found. That condition will fail for every other rock.

I think what you are looking for is almost identical in code, but plural.

You want:

GameObject[] rocks;

void Awake {
    rocks = (GameObject) FindGameObjectsWithTag("IronOre");
}

This will return not just the first object with that tag, but literally all of them. Note that GameObject is an array. Also, FindGameObjectsWithTag gives an array of Object, not GameObject, so I use a simple and shoddy cast (GameObject).

However, given that you are actually looking for the IronOre component, FindObjectsOfType is better.

IronOre[] rocks;

void Awake()
{
    rocks = (GameObject) FindObjectsOfType<IronOre>();
}

In Update, you will want to check the distance to all rocks, not just one.

void Update()
{
...blah blah blah
    for (int i=0; i<rocks.Length; i++)
    {
        float distance = Vector3.Distance(transform.position, rocks*.transform.position);*

if (input.GetKey… blah blah blah)
{
rocks*.IronOreHealth = -1;*
}
}
}
You could loop through OnTriggerEnter and check every objet against every one in your rocks array, but, there’s a much easier way of doing it.
void OnTriggerEnter(Collider other)
{
if (other.gameObject.GetComponent() != null) rockInRange = true;
}
Likewise for OnTriggerExit.
Good luck!