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