GetComponent problem!

Hello!
Got a problem with my GetComponent, i watched alot of tutorials but no one shows what i need.
I have GUI.Button that spawns a player, and the player costs Mana. So i made a mana meter. But how can i use getcomponent on a GUI button? All tutorials is about GameObjects.

I have 1 script for my mana and 1 script for my GUI.Button. I need to use GetComponent i my gui.button to get the mana script.

EDIT: I have both script attached to a gameobject…

Im sorry for bad explanation and my grammar, im tired… :stuck_out_tongue:

Thanks for fast help

What exactly is the Problem? GUI.Button returns true when it is clicked, so you can use it for an if and call var manaScript = GetComponent() when the condition (Button is clicked) is satisfied.

[ExecuteInEditMode]
public class GUI_Button1 : MonoBehaviour {

	public Texture2D buttonImage = null;
	public Rect position = new Rect(40, 40, 0 , 0);
	
	public Transform prefab;
	public GUISkin skin = null;
	
	MeadMeter script;
	
	
	void Awake () {
		
		script = GetComponent<MeadMeter>();
	}
	
	 private void OnGUI()
	{
		GUI.skin = skin;
		GUI.Button(position, buttonImage);
	
		if (GUI.Button (position,buttonImage)) {
			if (meadMeter >= mineCost){
		Instantiate(prefab);
		meadMeter -= mineCost;
				}
		
		}	
	}
}

This is the code, the Mead is Mana, but we dont use the name Mana :stuck_out_tongue:

And here i got the MeadMeter script

using UnityEngine;
using System.Collections;

public class MeadMeter : MonoBehaviour {
	
	
	public int meadMeter = 100;
	public int mineCost = 10;
	
	
	// Update is called once per frame
	void Update () {
		if(meadMeter < mineCost) {
			print("No more Mead");
		}
		GUI.Label(new Rect(450,0,Screen.width,Screen.height),
			"Mead: " + meadMeter);
	}
}

In your GUI_Button1 script you don´t use your script Variable, you should, at lines: 24 and 26.

in my GUI_Button1 script i use variables from my MeadMeter script, that is what i want to accomplish . Or what do you mean? Can you give me an example

Now i added MeadMeter.mineCost but now i got a error… : Assets/Scripts/GUI/GUI_Button1.cs(27,39): error CS0120: An object reference is required to access non-static member `MeadMeter.meadMeter’

All fixed now thanks!

NVM …
I got this error now

NullReferenceException: Object reference not set to an instance of an object
GUI_Button.OnGUI () (at Assets/Scripts/GUI/GUI_Button.cs:26)

I think this is the code you want, your problem was that you didn’t say which script the variable (mineCost and meadMeter) were coming from. i just typed it up in the forum so i haven’t tried it so it/I might be wrong.

[ExecuteInEditMode]
public class GUI_Button1 : MonoBehaviour {

    public Texture2D buttonImage = null;
    public Rect position = new Rect(40, 40, 0 , 0);
    public Transform prefab;
    public GUISkin skin = null;

    MeadMeter script;

    void Awake () {   
         script = GetComponent<MeadMeter>();
    }

     private void OnGUI()
    {
        GUI.skin = skin;
        GUI.Button(position, buttonImage);

        if (GUI.Button (position,buttonImage)) {
            if (MeadMeter.meadMeter >= MeadMeter.mineCost){
                 Instantiate(prefab);
                 MeadMeter.meadMeter -= MeadMeter.mineCost;
            }
        }   
    }
}

Yep fixed that but still gettin this error : NullReferenceException: Object reference not set to an instance of an object
GUI_Button.OnGUI () (at Assets/Scripts/GUI/GUI_Button.cs:26)

In this case, your script was unable to find your MeadMeter Component on the same GameObject as the GUI_Button1 script is.