How to remove an instantiated object on the scene?

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 :smiley:

You are setting isCreated = false; even if you do not right click on the object which makes it possible to create another object with left-click.

Change the isCreated flag only if the raycast has hit your object (and thus removed it from the scene)

 if (Input.GetMouseButtonDown (1) && isCreated)
                 {
                     Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
                     RaycastHit hit;                  
                     if (Physics.Raycast (ray, out hit, distance)) 
                     {
                         isCreated = false;
                         Destroy (hit.collider.gameObject);
                     }
             }

So to state it another way, when you hold right click down and then press left click you can instantiate the object again? So you are pressing both buttons at the same time? Easy fix then:

             //Just check that the right button is not down
             if (Input.GetMouseButtonDown (0) && !isCreated && !Input.GetMouseButton(1)) 
             {
                 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);
                 }
             }