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