3D Text over Game Object

Hello. I am trying to get a text mesh I created to copy itself and locate itself above an object when the mouse scrolls over said object.

What I have so far actually works but at the same time I get an error during run-time that says, “Cannot cast from source type to destination type.”

As I said, this code works, it’s just that error that confuses me.

This is the code:

using UnityEngine;
using System.Collections;

public class WarriorSelection : MonoBehaviour {
	
	public Transform target1;
	public Object target2;

void OnMouseEnter() {
	
	Vector3 ObjectPos = target1.transform.position;
		
	GameObject NameClone = (GameObject)Instantiate(target2, ObjectPos, Quaternion.identity);
	}

target1 is the object that I mouse over and target2 is the text mesh I display.

Also, I need help with destroying the text mesh during the function OnMouseExit() but whenever I try to use Destroy(NameClone), the compiler tells me that NameClone doesn’t exist in the current context.

Thanks for your help.

Well give it try

public Transform target1;
    public Object target2;
    private GameObject NameClone;

    void OnMouseEnter()
    {
        NameClone = Instantiate(target2, target1.transform.position, Quaternion.identity) as GameObject;
    }
    void OnMouseExit()
    {
        Destroy(NameClone);
    }

You can’t Instantiate a GameObject from an Object. The type of target2 should be GameObject (you rarely use Object for anything). In order to use a variable in another function, it must not be a local variable, but must be global. (BTW, you’d be better off sticking to the Unity scripting convention of using lowercase for variable names, and uppercase for classes and functions.)