Raycast never gets called

So, I made a script where something should be instantiated where the mouse is positioned.
However if (Physics.Raycast(ray)) {} never gets called.
I can’t seem to figure out why…
Here’s my current code : (this is nto the full code, but I think this is the only part needed, if not tell me. :slight_smile: )

                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

                if (Physics.Raycast(ray))
                {

                    if (Input.GetButtonDown("Fire1"))
                    {
                        Instantiate(towers*, Input.mousePosition, transform.rotation);*

Debug.Log(“Instantiated”);
towerRayBools = false;
}

}

@nobx101

you have written the if conditions in a wrong way, you are raycasting before pressing the button.

try this,

if(Input.GetMouseButtonDown(0))
		{
			Ray ry = Camera.main.ScreenPointToRay(Input.mousePosition);

			if(Physics.Raycast(ry, out hitinfo, Mathf.Infinity))
			{
				Instantiate(towers*, Input.mousePosition, transform.rotation);*
  •  		Debug.Log("Instantiated");*
    

_ towerRayBools = false;_
* }*

* }*