Hey, guys, I want to player choose specific locations in the game and by clicking button spawn buildings but I have 2 problems one is when I start to Instantiate next location I click also instantiate and second choosing location reference not set an instance of an object here's the 2 scripts
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class OnClick : MonoBehaviour {
[SerializeField]
public GameObject panel;
public static GameObject CurrentlySelectedGameObject = null;
void Update() /*selecting an object */{
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.tag == "EmptyGround")
{
if (panel != null)
{
panel.SetActive(true);
}
CurrentlySelectedGameObject = hit.collider.gameObject;
}
}
}
if (Input.GetMouseButtonDown(1))
{
if (panel != null)
{
panel.SetActive(false);
}
CurrentlySelectedGameObject = null;
}
}
}
^ this one for selecting object and set active building panel
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BuildManager : MonoBehaviour {
[SerializeField]
private GameObject TownBuilding;
private GameObject BuildingLocation;
[SerializeField]
private OnClick OnClickScript;
void Update() {
if (OnClick.CurrentlySelectedGameObject != null)
BuildingLocation.transform.position = OnClick.CurrentlySelectedGameObject.transform.position;
}
public void TownBuildingButton()
{
if (OnClick.CurrentlySelectedGameObject != null)
{
BuildingLocation = Instantiate(TownBuilding);
Destroy(OnClick.CurrentlySelectedGameObject);
}
}
}
^ this one for choosing building from panel and spawn in selected location
↧