"my script name" is a type, not a valid in the given context

error = “MeshScript” is a type, not a valied in the given context

        position = transform.position;

        //var meshScript = new MeshScript();
        var obj = GameObject.Find("Terain");
        var script = obj.GetComponent(MeshScript);
        script.MeshManipulate(position.x, position.y, position.z, scaler);
        Debug.Log(script);
    }

that where im getting an error from and I don’t understand why.
I would appreciate any help with this issue, thanks

1 Like

Seems like you’ve got a small typo here, you want to use getcomponent this way instead:
var script = obj.GetComponent<MeshScript>();

Also remember, C# is case sensitive, which means when you declare a variable, you have to consider upper and lower case of every character on it, when using it.

2 Likes

Both these should be valid:
obj.GetComponent();
obj.GetComponent(“MeshScript”);

The first one is preferred. The way you have it written is invalid syntax.

2 Likes

can anyone tell me why unity doesnt let you instantiate a class that inherits from “mono”, or if it does not the usual way by using the “new” keyword.

and thanks that helped.

That’s because those classes are components for game objects and their creation requires special handling done by unity in AddComponent method

ok, thanks that makes sense

for me it was another problem where I wrote

alertScript as = alert.GetComponent();

don’t use as as var name …

You’re necroing the post and even worse, with false information. This is a C# compiler thing, nothing to do with Unity or that specific method.

var just let’s the compiler infer the type, it’s no different and isn’t a source of errors. In your case above, it’d be “alertScript”.

Sure, you’re not supposed to use a reserved keyword as an identifier in C#.
How do you think the parser can differentiate what you mean?

Here is the list of all reserved keywords in C#, the bottom list are the contextual ones (such as value or yield), but if you can help it, avoid those as well.

If you really need to specify a name that is a reserved keyword, prepend @ sign to it, like so

AlertScript @as = alert.GetComponent<AlertScript>();

I would advise against doing this in 99.99% cases.

1 Like

Ah, now I read it again the OP was asking about using “as”. I thought that was a typo (typed twice) and they meant using “var”.

Still, the post is still a nercro/hijack … please no!