How add(create) a new gameobject on scene on mouse button click.

Hey guys… so, I am really new in code world and unity, so things are yet blurred to me. In fact I am learning yet how the things unroll and happens in the coding world. I had already looked here at Unity Community as in web as well but nothing works or fit to me.
What I want is after select a specific object in a list on UI, add that object to the scene I am in with mouse button click, what I mean is like we do in games like Cities Skylines, Prison Architect or other administration games we have around there where we can place objects on the world, or on the scene as I want.
Well, I would appreciate any help. I would like to ask for a explanation of what is happening on the code for I really learn where it is going to. So, thanks all.

Hello I see what u want. i Guess i can help you.

So first you need an array of your GameObjects that you want to instantiate depending on ur UI (cards)

	public GameObject[] g_plantPrefab;

So let’s say you have a tiles and you want to instantiate them on tiles…

Create a tile script and put this.

using UnityEngine;
using System.Collections;

public class MyTile : MonoBehaviour {

	private GameObject m_slot;
	public Color g_startingColor;

	// Use this for initialization
	void Start ()
	{

		g_startingColor = GetComponent<SpriteRenderer>().material.color;

	
	}
	

	public bool IsSlotTaken()
	{
		return m_slot != null;
	}

	public void AssignToSlot ( GameObject a_tower)
	{
		if ( a_tower != null)
		{
			// our slot is now taken
			m_slot =a_tower;

			// Reparent the tower to us. Make it our child.
			m_slot.transform.parent = this.transform;

			// Rest the position
			Vector3 startpos = new Vector3 (0 , 0 ,-0.1f);
			m_slot.transform.localPosition = startpos;
		}
	}

	public void DestroySlot()
	{
		if ( m_slot)
		{

			Destroy ( m_slot);
			m_slot = null;
		}
	}
}

Now to place the your card:

using UnityEngine;
using System.Collections;

public class MyTilePlace : MonoBehaviour
{

	private GameObject m_lastTile;
	public GameObject[] g_plantPrefab;
	public  int index;

	bool isCardSelected=  false;
	RaycastHit hitInfo;
	Ray ray;


	
	
	// Update is called once per frame
	void Update ()
	{

		Vector3 startpos = new Vector3 ( 0, 3 ,-0.149f);


		ray = GetComponent<Camera> ().ScreenPointToRay (Input.mousePosition);

		
		if (Input.GetMouseButton (0))
			{	
			if (m_lastTile != null) 
				
				{ 
					
				 if (m_lastTile.GetComponent<MyTile> ().IsSlotTaken () == false  )  
					{
						// instantiate your stuff on the tiles 
						GameObject plants = (GameObject)Instantiate (g_plantPrefab [index] , startpos , Quaternion.Euler ( 90 , 0 ,0));
						m_lastTile.GetComponent<MyTile> ().AssignToSlot (plants);
						// the player cant keep spawning plants, he needs to select another card again.
						isCardSelected = false;
					}

					}
				}
		}
	}

	void SetLastTile (GameObject a_tile)
	{
		if (m_lastTile != a_tile) {
			// if we have a last tile, rest its color
			if (m_lastTile != null) {
				// resrt its color
				m_lastTile.GetComponent<SpriteRenderer> ().material.color = m_lastTile.GetComponent<MyTile> ().g_startingColor;
			}

			// copy over the tile
			m_lastTile = a_tile;

			// if we have a new valid tile , set its color 
			if (m_lastTile != null) {
				m_lastTile.GetComponent<SpriteRenderer> ().material.color = Color.yellow;
			}
		}
	}

	public void onClickButton (int selectButton)
	{
		isCardSelected = true;

		index = selectButton;

	}

}


Noowww to your question ! :D!

Attach MyTilePlace script to ny empty gameobject or at ur camera. and on your cards or UI Each UI should be a Button. After that attach the your gameobject that you attach MyTilePlace to ur button and use the function "onClickButton" This will ask you to put a integer. use the integer that you want to use for your card and array. For example in ur array you attach your normal gameobject it's index is 0. so on your UI u want to instantiate (create ) this normal thing put 0 index.

Please vote if the answer help :D

You always just use:

var object : GameObject;

function GetKeyDown(Input.GetKey(KeyCode.Space)) {
Instantiate(object);
}

You can also set the position of the instantiated object as well.