Hello! I am making a building system to place turrets in my top down shooter but my problem is that when someone has selected a type of turret but decides to change the turret, when they select the new type of turret the previous turret is built. The problem I realized that I had was that I was already instantiating the object before placing it. What I need to do is make the object visible before placing without instantiating it so that when I change the type of turret it wont just be placed in a random place. Another possible solution is that if my mouse is over a UI I set TurretChoosen to false. The problem is that I don’t know how to check if my mouse is over UI.
Placing Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PointClickPlace : MonoBehaviour {
public Transform currentTurretPreview;
public Transform currentTurret;
public Camera MainCamera;
private OpenBulletChooser TurretChooser;
public bool TurretChoosen;
public int TypeTurretChoosen;
// Use this for initialization
void Start () {
TurretChooser = GetComponent<OpenBulletChooser>();
}
// Update is called once per frame
void Update () {
if (TurretChoosen == true)
{
Vector3 mPosition = Input.mousePosition;
mPosition = new Vector3(mPosition.x, mPosition.y, transform.position.y);
Vector3 Camera = MainCamera.ScreenToWorldPoint(mPosition);
currentTurretPreview.position = new Vector3(Camera.x, 1, Camera.z);
if (Input.GetMouseButtonDown(1) && TurretChoosen == true)
{
Instantiate(currentTurret, new Vector3(Camera.x, 1, Camera.z), currentTurret.rotation);
TurretChoosen = false;
}
}
}
public void SetTurret(GameObject Turret, GameObject TurretPrev)
{
currentTurretPreview = ((GameObject)Instantiate(TurretPrev)).transform;
currentTurret = Turret.transform;
TurretChoosen = true;
}
}