Using a gameObject reference as a variable?

For a project i am working on, I need a script that can tell what object the mouse is currently over. Not that hard to do, a simple ray cast. However, the objects that this script is attached to will be spawned in by the player, so to limit the amount of potential ray casts needed for each object to perform checks, I have simply opted to have one script attached to the camera that returns the object the mouse is over, and have all the spawned in objects inherit from it. However the code generates this error;

                          the script in question;

    using UnityEngine;
    using System.Collections;
    
    public class magnet : MonoBehaviour {
    	public mouseOver mouseHit; 
    	// Use this for initialization
    	void Start () {
    	
    	}
    	
    	// Update is called once per frame
    	void Update () {
    		GameObject mouseOver = mouseHit.objOver(); 
    		if (mouseOver == this); {
    			getTarget();
    		}
    	}
    
    	GameObject getTarget(){
    		bool state = false;
    		while (state == false){
    	    if (Input.GetButtonDown ("leftClick")) {
    			GameObject mouseOver = mouseHit.objOver();
    			}
    			***if (mouseOver != this){    

> mouseOver conflicts with the
> declaration type mouseOver, mouseOver
> is a type but is used like a variable

                              return  mouseOver;
    			}
    		if (Input.GetKeyDown("cancelAction")){
    				state = true;
    		}
    		}
    		return null;
    		}
    }


                                     and the script it inherits from;


using UnityEngine;
using System.Collections;

public class mouseOver : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	if (Input.GetButtonDown ("leftClick")) {
		 objOver ();
	}
	}

	public GameObject  objOver(){
		Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
		RaycastHit hit;
		
		if( Physics.Raycast( ray, out hit, 100 ) )
		{
			return  hit.transform.gameObject;
		}
		else
		{
			return null;
		}
}
}

i have tried using the new function like you would when using vectors as variables, but alas, no dice. Is this even possible? is it a logic error? A syntax error? Am i missing something basic? I am well versed in python, but new to C# and unity. Any help would be appreciated

This is certainly possible. But your way is pretty broken. Here is some better pseudo code.

public class MouseInputManager : MonoBehaviour {
    public static GameObject selectedObject;
 
    void Update () {
        if (Input.GetMouseButtonUp (0)) {
            Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );
            RaycastHit hit;
            Physics.Raycast( ray, out hit, 100 );
            selectedObject = hit.transform.gameObject;
            if (selectedObject) selectedObject.SendMessage("Selected");
        }
    }
}

public class SelectableObject : MonoBehaviour {
    public void Selected (){
        // Do something useful
    }
}

This is just the bare outline of a selection script. I suggest you also send the object a message when it is deselected. There is also better options for dealing with a click in empty space. You can also use the selectedObject variable to check what is currently selected. Useful for context based input.