Physics.Raycast not detecting objects between camera and player

I’m trying to make a script that disables the mesh renderers of objects that are between the player and the camera, so that if the camera moves through a wall, the player is still visible, since the object will be invisible (but still active). I’ve created a Physics.Raycast, but it isn’t detecting walls that go between the player and the camera. Here’s the script:

void FixedUpdate ()
	{
		//reenable all object renderers that have been disabled
		if (disabledObjects.Count > 0) 
		{
			Debug.Log ("list had at least one object in it");
			foreach (GameObject gObject in disabledObjects) {
				Renderer rend;
				rend = gObject.GetComponent <Renderer> ();
				rend.enabled = true;
			}
		}

		//clear the list, setting up to be refilled by new objects between camera and player.
		Debug.Log ("clearing list");
		disabledObjects.Clear ();

		//while loop to ensure each object detected by the raycast is disabled
		RaycastHit hitInfo;
		while (Physics.Raycast (transform.position, target.transform.position, out hitInfo)) 
		{
			Debug.Log ("there was something between the camera and the target");
			Renderer rend;
			rend = hitInfo.collider.GetComponent <Renderer> ();
			rend.enabled = false;
			disabledObjects.Add (hitInfo.collider);
		}
	}

What did I do wrong?

I’m pretty much completely new to Unity, please don’t eat me if this is a stupid question.

The while loop won’t do what you want.

Use Physics.RaycastAll instead of Physics.Raycast to get all the objects touched by the ray. In fact, disabling the mesh renderer won’t let the ray pass through your object.

Ignore my previous comment (I can’t delete it, since it hasn’t been approved yet). I solved the problem. The problem was that I thought a ray was drawn between two points, so I simply gave it the position of the camera and the player, but I solved it when I realized the second argument was direction, rather than another point. I also figured out that I had to turn gizmos on in order to use Debug.RaycastDraw.

I definitely would not have been able to figure out Physics.RaycastAll though, thanks for the help!