How do you create a gameobject that you can use to find multiple components?[Solved]

Hi, sorry if the title is poor.
I’m very new to programming and unity and absolutely terrible with terminology.
I am trying to create a simple idle like game with multiple buttons of different types to give a reward of money and exp.

Here’s some example of what i currently have:

public class BaseSkill : MonoBehaviour {


    public double level = 1, exp , nextLevel = 10;

    public GameObject building;

    public Text materialText, xpRewardText, cashRewardText, timerText;

}
public class MiningSkill : BaseSkill {

    double miningLvl = 1;
    double miningExp;
    double mineNextLevel = 15;
    void Start () 
    {

        Setup ();
    }

void Setup()
    {
        level = miningLvl;
        exp = miningExp;
        nextLevel = mineNextLevel;

        //materialText = building.FindGameObjectWithTag ("Material").GetComponent<Text> (); //Not Working.
        materialText = GameObject.FindGameObjectWithTag ("Material").GetComponent<Text> ();
        xpRewardText = GameObject.FindGameObjectWithTag ("XpReward").GetComponent<Text> ();
        cashRewardText = GameObject.FindGameObjectWithTag ("CashReward").GetComponent<Text> ();
        timerText = GameObject.FindGameObjectWithTag ("Timer").GetComponent<Text> ();
    }
}

I have a fishing skill which is basically the same.

I have the buildings; Mine and the Fishing Hut (from the image), They’re just duplicates of each other and their Text field tags are exactly the same.

What i would like to have is some way of differentiating these buttons from each other. This is where i’m finding it pretty hard to figure out, i thought it would be as simple as using:
materialText = building.FindGameObjectWithTag ("Material").GetComponent<Text> ();
And this is where my terrible knowledge of the terminology comes into play. I have only a very basic understanding of the Error code:

Assets/Scripts/New Folder/Skills/MiningSkill.cs(37,41): error CS0176: Static member `UnityEngine.GameObject.FindGameObjectWithTag(string)' cannot be accessed with an instance reference, qualify it with a type name instead infact i’m clueless, programming really makes me feel stupid. Does it mean, i cannot access GameObject building because it’s static and i’m trying to create a new instance reference of it ?

BASICALLY, i want to be able to create a GameObject (or something) which is equal to the game obj (or tag) each collection of buttons(buildings) are under, then i want to change their text fields independent of each other for every SkillClass i create(mining, fishing, carpentry).

I don’t know if this makes any sense to anyone because after 3 hours i think i’m overthinking everything, But any help will be greatly appreciated.

I can upload the whole project if needed, but it’s not pretty and most likely full of redundant code. :slight_smile:

First off, the “double” type is for large floating-point (having a decimal) number values, “float” is typically used normally for floating-point values where size isn’t a concern quite yet, and “int” is used for whole numbers. Going by context, pretty much all of your numbers need to be “ints”. You might want to spend a little time first studying the various value types used in C#- it’s boring, sure, but it’s very useful to know what tools you have to work with, and there aren’t that many types to memorize.

Second, try not to define multiple variables on a single line- not only does it make it harder to read, but it makes it harder to debug if there’s a problem. You’ll find that very often, just making your own code more readable fixes a lot of the little problems you’ve been having with it.

Third, the problem you’re having specifically is that you’re trying to reference a “static method” by referencing an instance object. “FindGameObjectWithTag” is a function of the GameObject class as a whole, not a specific GameObject, therefore you call it by using the class name, and not the name of an object, like so: GameObject.FindGameObjectWithTag(“TagName”).

It’s worth noting that if you’re looking for ALL objects with that tag name, you should add an “s” to make it “FindGameObjectsWithTag” which will return an array of GameObjects with that tag.

Do you notice how you can see and change the values of the public variables you’ve defined in your script directly in the inspector? You can also define a GameObject (say, called “button”) and leave it unassigned in your script. Then, in the inspector, you can drag the button object over onto the newly-created blank space next to “button” for that script. This will assign the button to the GameObject variable “button” in your code, and you can reference button.GetComponent().text in order to change the text of that button from the script.

Alternatively, you can do the same thing with a Transform or with a Text itself. If you define a UnityEngine.UI.Text object in the script as public, you can drag the button gameobject over to it in the inspector and it’ll allow you access the Text component in your script directly without needing to narrow it down to the component first (it references the Text component itself, not the whole GameObject).

Thank you for the reply! It has been a big help.
I have now fixed my issue by manually dragging and dropping the components in the inspector for each button. and using:

        materialText.GetComponent<Text> ();
        xpRewardText.GetComponent<Text> ();
        cashRewardText.GetComponent<Text> ();
        timerText.GetComponent<Text> ();

For the first point i also realize my mistake after checking through the unity reference and have converted them back to “int” for the time being whilst i implement some way of converting them to a smaller figure once i hit a large number. IE 1,000,000 = 1 million.

I also understand where i was going wrong with the error. Thank you for clarifying it, your reply was very easy to understand. It’s even color coded in MonoDevelop but i now know the real relationship of the .FindGameObjectsWithTag as a function of the GameObject class.

I shall mark this topic as solved.