Turning off collider in script C#

So pretty much the idea is that I do not want the collider on my given object active unless I actually need it to be. In this instance I want it off normally but to turn on when I activate my raycast to instantiate an object onto the surface. Now I haven’t tried this sort of thing before and while I found a few things they don’t seem to want to play nice. Not sure where the issue is and I was wondering if anyone might be able to steer me in the right direction.

public class Raycast : MonoBehaviour 
{

	public Transform Cheese;
	public Transform Sphere;
	public Transform Box;
	public Transform Capsule;
	public Transform Floor;

	void OnGUI()
	{
		if(GUI.Button(new Rect(10,10,150,100), "I make Cheese"))
		{
			Cheese = Box;
		}
		if(GUI.Button(new Rect(10,100,150,100), "I make sphere's"))
		{
			Cheese = Sphere;
		}
		if( GUI.Button(new Rect(10,200, 150,100), "I make Capsules'"))
		{
			Cheese = Capsule;
		}
		
	}

	void Update () 
	{

		Ray ray;
		RaycastHit hit;
		ray = Camera.main.ScreenPointToRay(Input.mousePosition);
		Debug.DrawRay(ray.origin, ray.direction * 100, Color.cyan);
		if(Input.GetButtonDown("Fire1"))
		{

			Floor.gameObject.collider.enabled = true;
			
				if (Physics.Raycast(ray, out hit, 100.0f))
				{

				if (hit.collider.tag=="Floor") 
					Instantiate(Cheese, hit.point, Quaternion.identity);
					Floor.gameObject.collider.enabled = false;
				}
		}

	}
}

Hello!

I believe that even if you are turning on and off the collider, the physics may not actually be updated until the next FixedUpdate() cycle.

I would suggest putting such GameObjects with colliders on their own layer. You can use PhysicsSettings to make everything else in the game ignore those colliders and make your Raycasts only hit such colliders.

Thanks, i’ll have to take a look into that.