Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Scripting' started by CodeingChris, May 8, 2019.

  1. CodeingChris

    CodeingChris

    Joined:
    Mar 31, 2018
    Posts:
    25
    error = "MeshScript" is a type, not a valied in the given context
    Code (CSharp):
    1.         position = transform.position;
    2.  
    3.         //var meshScript = new MeshScript();
    4.         var obj = GameObject.Find("Terain");
    5.         var script = obj.GetComponent(MeshScript);
    6.         script.MeshManipulate(position.x, position.y, position.z, scaler);
    7.         Debug.Log(script);
    8.     }
    that where im getting an error from and I don't understand why.
    I would appreciate any help with this issue, thanks
     
    nikhilesh0 likes this.
  2. FernandoHC

    FernandoHC

    Joined:
    Feb 6, 2018
    Posts:
    337
    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.
     
    Ch33ri0s and Ryiah like this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Both these should be valid:
    obj.GetComponent<MeshScript>();
    obj.GetComponent("MeshScript");

    The first one is preferred. The way you have it written is invalid syntax.
     
    Last edited: May 8, 2019
    Ch33ri0s and Ryiah like this.
  4. CodeingChris

    CodeingChris

    Joined:
    Mar 31, 2018
    Posts:
    25
    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.
     
  5. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,745
    That's because those classes are components for game objects and their creation requires special handling done by unity in AddComponent method
     
  6. CodeingChris

    CodeingChris

    Joined:
    Mar 31, 2018
    Posts:
    25
    ok, thanks that makes sense
     
  7. jaxx0rr

    jaxx0rr

    Joined:
    Aug 25, 2013
    Posts:
    23
    for me it was another problem where I wrote

    alertScript as = alert.GetComponent<alertScript>();

    don't use as as var name ...
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    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".
     
  9. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    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
    Code (csharp):
    1. AlertScript @as = alert.GetComponent<AlertScript>();
    I would advise against doing this in 99.99% cases.
     
    MelvMay likes this.
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    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!