Raycast Else not working.

I’m having an error in my raycast code. It seems that i cannot use else in raycast?
Or is there something else wrong in this code.

 public Transform player;
	
 public bool canSee = false;
  

    void OnWillRenderObject() {

        RaycastHit hit;

        if (Physics.Raycast(player.position, (transform.position - player.position).normalized, out hit)) {

            if (hit.collider.gameObject == gameObject) {
				canSee = true;
			

        }
            else{
              canSee = false;
				
		if(canSee == false){
		Debug.Log ("Player not in sight :(");
			
		}else{
		Debug.Log ("Player in sight attack >:D");

    }
   }
  }
}

The code is wrong: you’ve not closed the first else correctly. The code should be:

void OnWillRenderObject() {
    RaycastHit hit;
    if (Physics.Raycast(player.position, (transform.position - player.position).normalized, out hit)) {
        if (hit.collider.gameObject == gameObject) {
            canSee = true;
        } else {
            canSee = false;
        } // <= this closing brace was missing or misplaced
        if (canSee == false){
            Debug.Log ("Player not in sight :(");
        } else {
            Debug.Log ("Player in sight attack >:D");
        }
    }
}

Why are you placing this code inside OnWillRenderObject? This event only occurs when the object is inside the viewing angle of any camera. Is this a desired feature?