How to call for the nearest player or enemy to an object?

Hey all,

Just working on a MOBA game and I have a tower that I’m trying to make look at the minions(creeps) or the Player but I keep getting a few errors

Assets/Scripts/TowerScript.cs(76,24): error CS0165: Use of unassigned local variable closest' Assets/Scripts/TowerScript.cs(98,24): error CS0165: Use of unassigned local variable closest1’
Assets/Scripts/TowerScript.cs(51,20): error CS0161: `TowerScript.FindClosestEnemy()': not all code paths return a value

The script itself. First line is line 50.

//Find closest enemy to tower
    	GameObject FindClosestEnemy() 
    	{
    		//check if it's minion
    		if(Scanning == true)
    		{
    			//set gameobjects
            	GameObject[] gos;
            	gos = GameObject.FindGameObjectsWithTag("minion");
            	GameObject closest;
    			//create values
            	float distance = Mathf.Infinity;
            	Vector3 position = transform.position;
            	//proccess values
    			foreach (GameObject go in gos) 
    			{
            	    Vector3 diff = go.transform.position - position;
            	    float curDistance = diff.sqrMagnitude;
            	    
    				if (curDistance < distance) 
    				{
            	        closest = go;
            	        distance = curDistance;
            	    }
            	}
    			//return result
         	   	return closest;
    		}
    		//else check if it's player
    		else if(Scanning1 == true)
    		{
            	GameObject[] gos1;
            	gos1 = GameObject.FindGameObjectsWithTag("Player");
            	GameObject closest1;
            	float distance1 = Mathf.Infinity;
            	Vector3 position1 = transform.position;
            	
    			foreach (GameObject go1 in gos1) 
    			{
            	    Vector3 diff1 = go1.transform.position - position1;
            	    float curDistance1 = diff1.sqrMagnitude;
            	    
    				if (curDistance1 < distance1) 
    				{
            	        closest1 = go1;
            	        distance1 = curDistance1;
            	    }
            	}
         	   	return closest1;
    		}
        }

The problem is that there is a possibility that it will not return anything “TowerScript.FindClosestEnemy()': not all code paths return a value”, and it’s a function of type GameObject, that means that he must return something, even if it’s only return; or return null; you could make in the main code that if it’s null or something like that he will not attack anything.