Hi, I’m making a simple game where if I click the button, I can instantiate an object on the scene, but only once. If I right click on the object, it will be removed. However, my problem is when I instantiate an object and delete it, it works perfectly fine, but when I right click on the scene I can re-instantiate the object as many right clicks I did and if I right click on the scene, I can instantiate the object, which isn’t supposed to happen. Here is the following code snippet:
bool createStart = false;
bool isCreated;
public GameObject startPrefab;
if (createStart)
{
if (Input.GetMouseButtonDown (0) && !isCreated)
{
Vector3 p = Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 10.0f));
Instantiate (startPrefab, new Vector3 (Mathf.Round (p.x), Mathf.Round (p.y), 0.0f), Quaternion.identity);
isCreated = true;
}
if (Input.GetMouseButtonDown (1) && isCreated)
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
isCreated = false;
if (Physics.Raycast (ray, out hit, distance))
{
Destroy (hit.collider.gameObject);
}
}
}
Any help would be appreciated! I’m learning Unity and I hope you guys help me out ![]()