You probally just want to look at void OnGUI() ‘if(selectedBarracks != false)’ section
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class PlaceableBuilding : MonoBehaviour {
// This is the collider mesh for placing buildings
[HideInInspector]
public List<Collider> colliders = new List<Collider>();
private WorldCamera worldCamera;
// This is for activating CollectOil events
public delegate void StartCollectOil();
public static event StartCollectOil collectOil;
public static event StartCollectOil stopcollectOil;
private float timer;
// Deselect building
public bool selectedOilFarm = false;
public bool selectedBarracks = false;
// Deduct Resource on creation
public float DeductResource;
// Barracks
public Transform Barracks;
void Awake () // This controls the cost of buildings when they are instantiated
{
Mouse.buildingClicked += this.BuildingClicked; // when collectOil is called a call CollectOil below
Mouse.buildingUnClicked += this.BuildingUnClicked;
GameObject world = GameObject.Find("World");
DeductResource = world.GetComponent<ResourceControl>().Value;
if(this.gameObject.CompareTag("OilFarm"))
{
DeductResource = DeductResource - 15;
world.GetComponent<ResourceControl>().Value = DeductResource;
Debug.Log(DeductResource);
}
else if(this.gameObject.CompareTag("Barracks"))
{
DeductResource = DeductResource - 10;
world.GetComponent<ResourceControl>().Value = DeductResource;
Debug.Log(DeductResource);
}
}
void Update()
{
if(this.gameObject.transform.FindChild("OilRig"))
{
timer += Time.deltaTime;
if(timer >= 5)
{
Debug.Log ("Collecting Oil");
CollectOil();
timer = 0f;
}
}
}
void Destroy()
{
StopCollectOil();
}
public void OnGUI()
{
if(selectedOilFarm != false)
{
selectedBarracks = false;
GUI.Box(new Rect(Screen.width/2 -250, Screen.height/2 - 250, 500, 125), "No use yet");
if(GUI.Button(new Rect(Screen.width/2 - 150, Screen.height/2 - 200, 150,30), "Upgrade Oil Rig"))
{
Debug.Log ("ok");
}
} else
if(selectedBarracks != false) // THIS IS THE IF STATE MENT THAT PRODUCES 2 PREFABS
{
selectedOilFarm = false;
GUI.Box(new Rect(Screen.width/2 -250, Screen.height/2 - 250, 500, 125), "Barrracks");
if(GUI.Button(new Rect(Screen.width/2 - 150, Screen.height/2 - 200, 150,30), "Defender"))
{
GameObject defenderBot = Instantiate(Resources.Load("Prefabs/Units/DefenderBot", typeof(GameObject))) as GameObject;
Instantiate(defenderBot, new Vector3(this.transform.position.x, this.transform.position.y - 4, this.transform.position.z + 20), Quaternion.identity);
selectedBarracks = false;
} else if(GUI.Button(new Rect(Screen.width/2, Screen.height/2 - 200, 150,30), "Scout"))
{
GameObject scout = Instantiate(Resources.Load("Prefabs/Units/Scout", typeof(GameObject))) as GameObject;
Instantiate(scout, new Vector3(this.transform.position.x, this.transform.position.y - 4, this.transform.position.z + 20), Quaternion.identity);
selectedBarracks = false;
}
}
}
//fix this u dont want OnGUI()
public void BuildingClicked() // Ok a building has been clicked, lets find out what building it is.
{
if(this.gameObject.CompareTag("OilFarm"))
{
if(this.gameObject.transform.FindChild("Selector").gameObject.activeSelf)
{
selectedOilFarm = true; // Calls GUI Function above
Debug.Log("This is a Oilfarm");
}
} else
if(this.gameObject.CompareTag("Barracks"))
{
if(this.gameObject.transform.FindChild("Selector").gameObject.activeSelf)
{
selectedBarracks = true;
Debug.Log("This is a barracks");
}
}
}
public void BuildingUnClicked() // Building has been unclicked what should we do?
{
Mouse.DeselectGameobjectsIfSelected();
selectedOilFarm = false;
selectedBarracks = false;
}
public void CollectOil()
{
if(collectOil != null)
{
collectOil();
GameObject oilFarm = this.transform.FindChild("OilRig").gameObject as GameObject;
//oilFarm.animation.Play();
}
}
public static void StopCollectOil()
{
if(stopcollectOil != null)
stopcollectOil();
}
// Collider mesh for building placement
void OnTriggerEnter(Collider c)
{
if(c.tag == "Building")
{
colliders.Add(c);
}
}
void OnTriggerExit(Collider c)
{
if(c.tag == "Building")
{
colliders.Remove(c);
}
}
}
Basically when i press the button a prefab is produced at the expected location but one is also produced where i last deleted the prefab in unity scene window