Old GUI into new GUI problem.

Hello,

first of all:
I want to instiate an Object when Clicking on my “Create Building” Button and place this instiated Object anywhere on the Ground. Then I want to be able to create(instantiate) my Workers/Monsters

I have made a prefab; tagged it as Building, layers is Building too, attached the prefab to the BuildingManager script on my Camera.

I have made 2 scripts I attached to the Camera:
first the BuildingManager

which includes:

using UnityEngine;
using System.Collections;

public class BuildingPlacement : MonoBehaviour {

	private Transform currentBuilding;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void FixedUpdate () {
	
		if (currentBuilding != null) {
			Vector3 m = Input.mousePosition;
			m = new Vector3(m.x,m.y,transform.position.y);
			Vector3 p = camera.ScreenToWorldPoint(m);
			currentBuilding.position = new Vector3(p.x,0,p.z);
		}
 }
			    public void SetItem(GameObject b) {
				currentBuilding = ((GameObject)Instantiate (b)).transform;
			}

	
}

the public void SetItem is attached to my “create Button” on the (onclick) function. This works perfectly.
→ on playmode, I click on the Button and it’s creating an Object.

The Other script is BuildingPlacement
which includes:

using UnityEngine;
using System.Collections;

public class BuildingPlacement : MonoBehaviour {

	private Transform currentBuilding;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void FixedUpdate () {
	
		if (currentBuilding != null) {
			Vector3 m = Input.mousePosition;
			m = new Vector3(m.x,m.y,transform.position.y);
			Vector3 p = camera.ScreenToWorldPoint(m);
			currentBuilding.position = new Vector3(p.x,0,p.z);
		}
 }
			    public void SetItem(GameObject b) {
				currentBuilding = ((GameObject)Instantiate (b)).transform;
			}

	
}

Now I am creating an Object when clicking on my createButton and automatically drag it anywhere, but I cannot click on the ground to instantiate it finally. It only works when clicking on the createButton again but then it’s only one Position what I am able to create anything…

I tried this Tutorial which includes the old GUI and wanted to make it into the new GUI


Thanks for reading. If anything is unclear,
I will answer asap.
~ Flowered

OK @Flowered finally got chance to look at this, basically had to rewrite it but not totally dissimilar to something I’ve done before so most of the script was already written. Now I used an image for dragging round as I’d created a fairly large cube as a test item but you could create a 3D object just as in that video if you prefer that.

This should give you an idea of how to go about it…

using UnityEngine;
using UnityEngine.UI;				// Mmmpies added
using System.Collections;

public class CreateSpawn : MonoBehaviour {

	//The building prefabs, this is just an array of GameObjects that is set through the inspector.
	public GameObject[] Buildings;
	public Image[] BuildingIcons;			// Mmmpies added
	public float[] BuildingHeight;			// Mmmpies added

	public RectTransform ParentTransform;		// Mmmpies added
	public Camera MyCamera;				// Mmmpies added


	//This is the layer that the terrain is on
	public LayerMask acceptableLayer;
	//Defines wether we're checking for a valid position
	private bool currentlyBuilding = false;
	//The position at which we'll spawn the building		
	private Vector3 position;
	//The building we're currently placing		
	private GameObject currentBuilding;

	private Vector3 mouseStartPos;				// Mmmpies added
	private Image currentImage;				// Mmmpies added
	private int bldIndex;					// Mmmpies added

	// Mmmpies added/edited vvvvvvvvvvvvvvvvvvvvvvvvv
	public void OnMouseDown() 
	{
		if(currentlyBuilding)
		{
			Ray ray = MyCamera.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit = new RaycastHit();

			if(Physics.Raycast(ray, out hit, Mathf.Infinity, acceptableLayer)){
				position = hit.point;
			}		
			else
				position = Vector3.zero;

			currentlyBuilding = false;
			SpawnBuilding();
		}
	}
	// Mmmpies added/edited ^^^^^^^^^^^^^^^^^^
	
	// Mmmpies added vvvvvvvvvvvvvvvvvvv
	void Update()
	{
		if(currentlyBuilding)
		{
			Vector3 currentPos = Input.mousePosition;
			Vector3 diff = currentPos - mouseStartPos;
			Vector3 pos = new Vector3(mouseStartPos.x + diff.x + 45, mouseStartPos.y + diff.y - 45, mouseStartPos.z);
			currentImage.transform.position = pos;
		}
	}
	// Mmmpies added ^^^^^^^^^^^^^

	//The will be called from your UI button.
	public void AssignBuilding(int buildingIndex)
	{
		bldIndex = buildingIndex - 1;				// Mmmpies added
		currentBuilding = Buildings[bldIndex];			// Mmmpies edited
		currentlyBuilding = true;

		// Mmmpies added vvvvvvvvvvvvvvvvvvvvvvvvvvvvv
		mouseStartPos = Input.mousePosition;
		Image newIcon = (Image)Instantiate(BuildingIcons[buildingIndex - 1], mouseStartPos, Quaternion.identity);
		newIcon.transform.SetParent(ParentTransform, false);
		newIcon.transform.localPosition = new Vector3(1, 1, 1);

		Vector3 diff = newIcon.transform.position - mouseStartPos;
		newIcon.transform.position = newIcon.transform.position - diff;

		currentImage = newIcon;
		// Mmmpies added ^^^^^^^^^^^^^^^^^^^
	}

	//Spawn the building if the current position is valid.
	public void SpawnBuilding(){
		if(position != Vector3.zero){
			Destroy(currentImage,0);											
			float tempFloat = position.y + BuildingHeight[bldIndex];
			Vector3 tempPos = new Vector3(position.x, tempFloat, position.z);
			GameObject newGO = (GameObject)Instantiate(currentBuilding,
			                                           tempPos, 
			                                           currentBuilding.transform.rotation);
			newGO.name = "A-Building";
		}
	}
}

I moved the script onto the terrain and set the AcceptableLayer to default for testing. You need it to be on the terrain so the OnMouseDown works. Because of that I added the public camera so the raycast knows which camera to use and added the RectTransform for your canvas so it can work out where to move the image to.

You click the UI button (which you’ll need to reset the OnClick after you move the script) and an Image appears, move the image around the screen and click again to place the object. I also added a BuildingHeight array to offset the instantiate position so the building doesn’t appear in the middle of the terrain.

EDIT

And this is brief video of the results:

Drag-Image-Instantiate-Object