My images are overlapping each other

Using Unity 2D

I have a game that uses hot keys, pressing “X” brings up the roads template, and “Z” brings up the building template. The problem is, which you will see in the diagrams below, after pressing the “Z” key once, then pressing the “X” key the road template is on top of the building template picture. You can move the mouse to where you want to place the template, then left click to place it.

My “template” or “blueprint” if you will, is by lowering the alpha color channel and then when you place it in the game world by clicking the mouse button, the image turns into a solid color (raising the alpha color channel)

They do have a sprite renderer, layer is set to default so not a UI element.

Diagram A: After pressing “Z”:

6208851--682098--upload_2020-8-15_20-39-4.png

Diagram B: After pressing “Z” first then while the selection is still open, you press “X” (notice how the road (blue) is on top of the building template image.

6208851--682101--upload_2020-8-15_20-40-22.png

How do I fix it so you dont have to clear the current selection (do that by pressing the ESC key) so they dont overlap? Diagram C:

6208851--682104--upload_2020-8-15_20-46-4.png

I dont do a very good job explaining, so I hope the diagrams helped. Below is the script for the building hot key which is “Z”

//CODING THE MOUSE POSITION, THE Z KEY, AND MOUSE TO MOVE THE SELECTED TEMPLATE AROUND AND THE Z TO OPEN THE SELECTION
    private void Update()
    {
        Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector2 spawnPos = new Vector2(Mathf.Round(mousePos.x), Mathf.Round(mousePos.y));

        if (Input.GetKeyDown("z") && isAnObjectSelected == false) //doesnt have an object selected

        {
            currentlySelectedObject = (GameObject)Instantiate(selectableObjects[selectedObjectInArray], spawnPos, Quaternion.identity); //create but also storing
            isAnObjectSelected = true; //to now say that we got one out
        
        }
 
        }

        {

     //----------------------------------------------------------------------------------------------

            //CODING THE ESCAPE KEY TO CLEAR THE SELECTED OBJECT
            if (Input.GetKeyDown(KeyCode.Escape) && isAnObjectSelected == true) //if object is selected, and we press the "escape" key, it will clear it
            {
                Destroy(currentlySelectedObject); //deletes the selected template
                isAnObjectSelected = false;
                selectedObjectInArray = 0; //resets it back to just the mouse cursor with no template
            }

        
          //-------------------------------------------------------------------------------------------------
        
         
            //CODING THE SCROLL WHEEL TO SCROLL THROUGH THE DIFFERENT OBJECTS IN THE SELECTION
            if (Input.GetAxis("Mouse ScrollWheel") > 0 && isAnObjectSelected == true)
            {
                selectedObjectInArray++;
                if (selectedObjectInArray > selectableObjects.Length - 1)
                {
                    selectedObjectInArray = 0;
                }
                Destroy(currentlySelectedObject);
                currentlySelectedObject = (GameObject)Instantiate(selectableObjects[selectedObjectInArray], spawnPos, Quaternion.identity);
            }

            else if (Input.GetAxis("Mouse ScrollWheel") < 0 && isAnObjectSelected == true)
            {
                selectedObjectInArray--;

                if (selectedObjectInArray > selectableObjects.Length - 1)
                {
                    selectedObjectInArray = 0;
                }
                Destroy(currentlySelectedObject);
                currentlySelectedObject = (GameObject)Instantiate(selectableObjects[selectedObjectInArray], spawnPos, Quaternion.identity); //if brackets are correct and still get a red underline under one of them, check the end of the statement to see if it has a semi-colon

            }
        }

You said a lot about “pictures” and “templates” but that doesn’t mean much to me personally. What kind of objects are we talking about here? Are these UI elements on a canvas? 2D sprites in the game world? 3D objects?

What does “Clicking X” mean?

im using Unity 2D.

Pressing the “X” key brings up the road template, then clicking on a spot on the map with mouse button places the road

Pressing the “Z” key brings up the building template and the same thing happens when you click mouse button.

Problem is, when you press the “X” key after you already pressed the “Z” key, the road template is on top of the building template as we see in diagram B.

I dont wanna have to do it where pressing “Z” disables the “X” key to fix that problem. No one is gonna want to press “escape” each time you want to bring up the “X” template

Again I have no idea what a “template” is? Are these game world sprites? Are they UI elements?

Anyway… It seems you are instantiating and destroying objects to draw these “templates” Why not just destroy whatever the current template is when you press Z or X before creating the new template object?

well, my “template” or “blueprint” is by lowering the alpha color channel and then when you place it in the game world by clicking the mouse button, the image turns into a solid color (raising the alpha color channel)

They do have a sprite renderer, layer is set to default so no, not UI.