Check another script to start a OnGUI fucntion.

Hello. I’m trying to access a other C# script I’ve created. I want to call a variable from this script(EnemyHealth), but how do I manage that? Once I have access to that variable I would like to call another function in a different script. I’ll post both script here to give you a better view on how I’ve scripted it all.

EnemyHealth:
using UnityEngine;
using System.Collections;

public class EnemyHealth : MonoBehaviour {
	public int maxHealth = 100;
	public int curHealth = 100;
	
	public float healthBarLenght;

	// Use this for initialization
	void Start () {
		healthBarLenght = Screen.width / 2;
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	void OnGUI() {
		GUI.Box(new Rect(10, 40, healthBarLenght, 25), curHealth + "/" + maxHealth);
	}
	
	public void AdjustCurrentHealth(int adj) {
		curHealth += adj;
		
		if(curHealth > maxHealth)
			curHealth = maxHealth;
		
		if(maxHealth < 1)
			maxHealth = 1;
		
		if(curHealth < 0)
			curHealth = 0;
		
		healthBarLenght = (Screen.width / 2) * (curHealth / (float)maxHealth);
	}
}

and the TutorialMenu:

using UnityEngine;
using System.Collections;

public class TutorialMenu : MonoBehaviour {
	public bool DisplayText1 = false;			//Welcome to Bow Brothers! This tutorial will try to teach you how to: Move, attack and loot, as well as simultaneously trying to make you familiar with the crafting system.
	public bool DisplayText2 = false;			//See the white Cube infront of you? He's going to attack you. If you want to survive you'll have to fight back. To fight back use the LMB button.
	public bool DisplayText3 = false;			//Well done! Now, walk up to the dead cube and press E. This way you can loot his body.
	public bool DisplayText4 = false;			//Seems like this cube was carrying a bucket and an axe. You can equip the axe by pressing 'I' on your keyboard and right-click on the axe, then select 'equip' to equip the axe.
	public bool DisplayText5 = false;			//Not only does the axe provide extra melee damage, it's also able to cutdown trees. Like that one over there. Walk up to it and press 'E'.
	
	public bool ContinueButton1 = false;
	public bool ContinueButton2 = false;
	public bool ContinueButton3 = false;
	public bool ContinueButton4 = false;
	public bool ContinueButton5 = false;
	
	void OnLevelWasLoaded(int level) {
		if(level == 1) {
			Time.timeScale = 0;
			DisplayText1 = true;
			ContinueButton1 = true;
		}
	}
	
	public void StartToDisplayText2() {
	}
	
	public void StartToDisplayText3() {
	}
	
	public void StartToDisplayText4() {
	}
	
	public void StartToDisplayText5() {
	}
	
	//Activate DisplayText2
	public void StartDisplayText2() {
		DisplayText1 = false;
		DisplayText2 = true;
		DisplayText3 = false;
		DisplayText4 = false;
		DisplayText5 = false;
	}
	
	//Activate DisplayText3
	public void StartDisplayText3() {
		DisplayText1 = false;
		DisplayText2 = false;
		DisplayText3 = true;
		DisplayText4 = false;
		DisplayText5 = false;
	}
	
	//Activate DisplayText4
	public void StartDisplayText4() {
		DisplayText1 = false;
		DisplayText2 = false;
		DisplayText3 = false;
		DisplayText4 = true;
		DisplayText5 = false;
	}
	
	//Activate DisplayText5
	public void StartDisplayText5() {
		DisplayText1 = false;
		DisplayText2 = false;
		DisplayText3 = false;
		DisplayText4 = false;
		DisplayText5 = true;
	}

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		
	}
	
	void OnGUI() {
		//Start Text 1
		if(DisplayText1) {
			GUI.Label(new Rect(10, 70, Screen.width / 2, Screen.height / 2), "Welcome to Bow Brothers! This tutorial will try to teach you how to: Move, attack and loot, as well as simultaneously trying to make you familiar with the crafting system. Press the 'Return' button to continue.");
		}
		
		//Start Text 2
		if(DisplayText2) {
		}
		
		//Start Text 3
		if(DisplayText3) {
		}
		
		//Start Text 4
		if(DisplayText4) {
		}
		
		//Start Text 5
		if(DisplayText5) {
		}
		
		//Activate the Different Continue Buttons.
		//Start ContinueButton1
		if(ContinueButton1) {
			if(GUI.Button(new Rect(10, 320, 200, 50), "Continue")) {
				Time.timeScale = 1;
				DisplayText1 = false;
				ContinueButton1 = false;
			}
		}
		
		//Start ContinueButton2
		if(ContinueButton2) {
			if(GUI.Button(new Rect(10, 320, 200, 50), "Continue")) {
			}
		}
		
		//StartContinueButton3
		if(ContinueButton3) {
			if(GUI.Button(new Rect(10, 320, 200, 50), "Continue")) {
			}
		}
		
		//StartContinueButton4
		if(ContinueButton4) {
			if(GUI.Button(new Rect(10, 320, 200, 50), "Continue")) {
			}
		}
		
		//StartContinueButton5
		if(ContinueButton5) {
			if(GUI.Button(new Rect(10, 320, 200, 50), "Continue")) {
			}
		}
				
	}
}

I know there should be other and more simple ways to do this. But I’m new to C# and Unity and I’m very proud on what I’ve made so far :wink:

I hope these scripts won’t be used for other purposes, except to review them.

If both scripts are attached to the same gameObject, you can call

gameObject.GetComponent<EnemyHealth>().healthBarLenght 

in the TutorialMenu script.

Also, you should learn about arrays, as that would cut the size of TutorialMenu by 4/5th and the code way more readable.

MonoBehavior references can be assigned in the editor.

It would look like:

public EnemyHealth enemyHealth;

In code, and then you’d drag the appropriate EnemyHealth script into that field in the inspector.

Another option is to have a variable for the enemy itself and then get the EnemyHealth component out that way.

public GameObject enemy;
private EnemyHealth enemyHealth;

void Start() {
    enemyHealth = enemy.GetComponent<EnemyHealth>();
}

In this case, you just have to assign the enemy game object to the TutorialScript’s enemy variable.