Why is this instantiate producing 2 prefabs?

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

Hi there,

So what looks to be your problem is these lines of code:
GameObject defenderBot = Instantiate(Resources.Load("Prefabs/Units/DefenderBot", typeof(GameObject))) as GameObject;

GameObject scout = Instantiate(Resources.Load("Prefabs/Units/Scout", typeof(GameObject))) as GameObject;

Instantiate will still run even if it is an assignment to a variable. Instead of assigning it to a variable like that just set up some Transform variables at the top of your script and assign the prefabs in the editor to those fields and then pass those to an Instantiate call. :slight_smile:

Hope this helps,

Hunter

ok, this is a bummer looks like i need to set up some delegates and events because this script is attached to a object that is spawned ingame, therefore i cant assign the variable in editor

I.e. I spawn a prefab ‘Barracks’ with this script attached, so i dont think i can set the variables in editor can i?

Editoh gosh massive derp just relised i can Edit

Thanks for the help il let u know if works :slight_smile:

If it is a prefab you can assign the fields in the editor I believe.
Just select the prefab and drag the other prefabs into the fields.

[Edit] Just saw your edit[/Edit]

Good luck!

Works like an absolute charm! and cleaned up my script Thanks!

Any time :slight_smile: