c# calling function from different script?

As I commented in the player script I’m trying to call the inventory function inside the HUD class from Player.cs. I have looked all over online and nothing is working, any suggestions?

Player.cs

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {
	public Transform firstperson;
	public Camera fps_cam;
	public Camera tps_cam;
	public bool inInventory = false;
	private Vector3 moveDirection = Vector3.zero;
	
	void Start() {
		fps_cam.enabled = true;
		tps_cam.enabled = false;
	}
	
	void Update (){
		if(Input.GetButtonDown("Fire1")) {
			Screen.lockCursor = true;
		}
		Controls();
	}
	
	void Controls() {
		//Change POV
		if(Input.GetKeyDown(KeyCode.KeypadEnter)) {
			fps_cam.enabled = !fps_cam.enabled;
        	        tps_cam.enabled = !tps_cam.enabled;
		}
		if(Input.GetKeyDown(KeyCode.I)) {
			//trying to call HUD.inventory(true) here
		}
	}
}

HUD.cs

using UnityEngine;
using System.Collections;

public class HUD : MonoBehaviour {
	
	void Update() {
		
	}
	
	public void Inventory(bool open) {
		if(open)
			print ("test");
	}
	
	void OnGUI() {
		
}

Something like this:

var hud = gameObject.GetComponent("HUD");
hud.inventory(true);

If your HUD instance is part of another game object you’ll need to get that gameobject via something like the tag name and then do the same GetComponent call.

I’m using C# not JS so its a little diff than just defining var but its pretty close. I actually figured it out, took me some hours but I found out something that works :slight_smile:

GameObject obj = GameObject.Find ("Main Camera");
HUD hud = obj.GetComponent<HUD>();
hud.Inventory(true);

Thanks anyways! :smile: