error CS1061 when there shoulnd't be

Ok, here’s the deal:
Here is the first version of my code:

NormalCube OtherScript = WhatUp.GetComponent(typeof(NormalCube))
as NormalCube;

WhatUp is a variable which contains a GameObject.

NormalCube is a class, which is also a component in the WhatUp GameObject

UnderCursor is a public bool variable in the NormalCube class (which as been created with:

public bool UnderCursor = false;

Now, I’ve put the

WhatUp.GetComponent(typeof(NormalCube)) as NormalCube

In a functions which has GameObject parameter. It goes like:


Those two returns the same damn thing. In the end, my code looks like:

 var OtherScript = Functions.GetScript(WhatUp);

BUT, I get an error which goes like:

Assets/Scripts/Athlete.cs(143,35): error CS1061: Type `UnityEngine.MonoBehaviour' does not contain a definition for `UnderCursor' and no extension method `UnderCursor' of type `UnityEngine.MonoBehaviour' could be found. Are you missing an assembly reference?

I have no idea what to do, and I don’t want to have a long Switch statement with all of the possible Scripts it can return, because I use it in many other Scripts, and that’s why I have a Function. Help will be really appreciated.

@EBHero Since most scripts in Unity will inherit from MonoBehaviour, you could change the definition of GetScript to be a generic:

public static T GetScript<T>(GameObject obj) where T : MonoBehaviour
    return obj.GetComponent<T>();

For example, if you attempt to assign to an object of type NormalCube using this method and the return value is null, then no script of that type existed on the GameObject you passed to the method. In other words, do check the object for whether or not its null before you attempt to use it.