Instantiate object from GUI? (C#)

I’m making an RTS game. For this game, I need to be able to train units using buttons on the GUI. The way I have things, when I click the train rust reaper button it calls the function trainUnit() in the GUI script, which calls the trainRustReaper() function of the HangerScript attached to a transform that I assign in the inspector window to the frogFace variable. However, whenever I press the train rust reaper button in game, i get a runtime error in the console saying that frogFace is not assigned. Could someone please help me with this? Here’s the code for GUIScript.cs:

using UnityEngine;
using System.Collections;

public class GUIScript : MonoBehaviour 
{
	public GUISkin mySkin;
	
	public Texture2D buildTex;
	public Transform mainBase;
	public Transform farm;
	public Transform hangar;
	public Transform rightWing;
	public Transform leftWing;
	public GUITexture trainingLoading;
	public GUITexture trainingComplete;
	
	public Transform palace;
	public Transform apartments;
	public Transform factory;
	public Transform barracks;
	public Transform reactor;
	
	public Transform frogFace;
	
	public bool human = true;
	
	private bool train = false;
	private bool build = false;
	private bool level1 = true;
	private bool buildWing = false;

	void Start () 
	{}
	
	void Update () 
	{}
	
	void OnGUI()
	{
		GUI.skin = mySkin;
		GUI.Box( new Rect( 0, Screen.height-150, Screen.width, 150 ), "" );
		if( !build  !train )
		{
			if( GUI.Button(new Rect(10, Screen.height-140, (Screen.width/2)-20, 130), "Build Structures (B)") || Input.GetKeyDown("b") )
			{
				build = true;
				train = false;
			}
			else if( GUI.Button(new Rect((Screen.width/2)+10, Screen.height-140, (Screen.width/2)-20, 130), "Train Units (T)") || Input.GetKeyDown("t") )
			{	
				train = true;
				build = false;
			}
		}
		else if( build  !train )
		{
			if( level1 )
			{
				if( GUI.Button(new Rect(10, Screen.height-140, 130, 130), "Place Wing (W)") || Input.GetKeyDown("w") )
				{
					level1 = false;
					buildWing = true;
				}
				else if( GUI.Button(new Rect(150, Screen.height-140, 130, 130), "Place Farm (F)") || Input.GetKeyDown("f") )
				{}
				else if( GUI.Button(new Rect(290, Screen.height-140, 130, 130), "Place Hangar (H)") || Input.GetKeyDown("h") )
				{}
				else if( GUI.Button(new Rect(430, Screen.height-140, 130, 130), "Place Base (B)") || Input.GetKeyDown("b") )
				{}
				else if( Input.GetKeyDown("escape") )
				{
					level1 = false;
					build = false;
					train = false;
				}
			}
			if( buildWing )
			{
				if( GUI.Button(new Rect(10, Screen.height-140, 130, 130), "Right Wing(R)") || Input.GetKeyDown("r") )
				{
					createWing( true );
					buildWing = false;
				}
				else if( GUI.Button(new Rect(150, Screen.height-140, 130, 130), "Left Wing(L)") || Input.GetKeyDown("l") )
				{
					createWing( false );
					buildWing = false;
				}
				else if( Input.GetKeyDown("escape") )
				{
					buildWing = false;
					level1 = true;
					build = true;
					train = false;
				}
			}
		}
		if( train  !build)
		{
			if( GUI.Button(new Rect(10, Screen.height-140, 130, 130), "Build Rust Reaper (R)") || Input.GetKeyDown("r") )
			{
				trainUnit();
			}
			else if( GUI.Button(new Rect(150, Screen.height-140, 130, 130), "Build Skimmer (S)") || Input.GetKeyDown("s") )
			{}
			else if( Input.GetKeyDown("escape") )
			{
				train = false;
				build = false;
			}
		}
	}
	
	public void createWing( bool boo )
	{
		if( boo )
			Instantiate( rightWing, Input.mousePosition, Quaternion.identity );
		else
			Instantiate( leftWing, Input.mousePosition, Quaternion.identity );
	}
	
	public void createFarm( int i )
	{
		if( i == 0 )
		{}	//farm facing north
		else if( i == 1 )
		{}	//farm facing east
		else if( i == 2 )
		{}	//farm facing south
		else if( i == 3 )
		{}	//farm facing west
	}
	
	public void trainUnit()
	{
		print("lkjlkj");
		frogFace.GetComponent<HangerScript>().trainRustReaper();
//		for( int x = 0; x < 8; x++ )
//		{
//			GameObject hanger = GameObject.Find("Hangar1");
//			while( hanger.GetComponent<HangerScript>().training )
//				hanger = gameObject.Find("Hanger1");
//			hanger.GetComponent<HangerScript>().train( unitName );
//		}
	}
}

Here’s HangerScript.cs:

using UnityEngine;
using System.Collections;

public class HangerScript : MonoBehaviour 
{
	public GUITexture trainingLoading;
	public GUITexture trainingComplete;
	
	public Transform rustReaper;
	public Transform skimmer;
	public Transform gaussCannon;
	public Transform mobileSAM;
	public Transform freedomFighter;
	public Transform flyingSquirrel;
	public Transform helicoptor;
	
	private bool training = false;
	private float rustReaperTime = 5F;
	private float skimmerTime = 5.5F;
	private float gaussCannonTime = 15F;
	private float mobileSAMTime = 10F;
	private float freedomFighterTime = 8F;
	private float flyingSquirrelTime = 10F;
	private float helicoptorTime = 8.5F;
	private string currentUnit;
	
	// Use this for initialization
	void Start ()
	{}
	
	// Update is called once per frame
	void Update () 
	{}
		
	public void trainUnit( string unitName )
	{
		trainingLoading.GetComponent<ObjectLabel>().target = this.transform;
		trainingComplete.GetComponent<ObjectLabel>().target = this.transform;
		trainingComplete.pixelInset = new Rect( trainingComplete.pixelInset.x, trainingComplete.pixelInset.y, 0, 5 );
		training = true;
		currentUnit = unitName;
	}
	
	public void trainRustReaper()
	{
		print("ffffff");
		Instantiate( rustReaper, transform.position+new Vector3( 0, 0, -10 ), Quaternion.identity );
	}
}

Any help is appreciated!

you say that the error tells you that trainingHanger is not assigned, but i’m not able to find any trainingHanger variable in your script so are you sure that it is in one of the above scripts the problem is?

and if it is in one of the above scripts which the problem lies can you then tell what line it is on?

Oh sorry, I renamed it to frogFace for debugging purposes. It doesn’t work with that name either, though. I’ll edit the first post.

bump

bump.

Can you post the results of the following

public void trainUnit()
{
   print("lkjlkj");
   print(frogFace != null);
   print(frogFace.GetComponent<HangerScript>() != null);
   frogFace.GetComponent<HangerScript>().trainRustReaper();

And the exact error message with the complete stacktrace.

So I’m getting this error: NullReferenceException: Object reference not set to an instance of an object
GUIScript.trainUnit () (at Assets/GUI/GUIScript.cs:210)
GUIScript.OnGUI () (at Assets/GUI/GUIScript.cs:123). Before it, though, it prints lkjlkj, then true, then false. Apparently I don’t have a HangerScript attached to the transform in frogFace… Looking in the inspector, I can see that this is the case. If I actually have a HangerScript attached, though, it works very well. Thanks!