- Home /
Expression denotes a 'type'
"Expression denotes a 'type' where a 'variable' 'value' or 'method group' was expected"
It seems that there are many of these posts but they all seem rather specific to the code, or rather I haven't seen a good explanation of it but anyway; Could someone please find out whats wrong with my code and if possible provide a link to or an explanation of this error.
GameObject Arrow = Instantiate(projectile, bow.transform.position, bow.transform.rotation ) as GameObject;
Arrow.GetComponent(ArrowFlight).currentTarget = target;
Many thanks.
Answer by liszto · Nov 14, 2012 at 12:16 AM
First of all if you code in C# you must do this :
Arrow.GetComponent<ArrowFlight>().currentTarget = target;
Cause it's cleaner.
Second :
ArrowFlight ArrowFlightObj = (ArrowFlight) Arrow.GetComponent<ArrowFlight>();
ArrowFlightObj.currentTarget = target;
Or something like that ;)
Thanks for taking the time! However using your script yields an error: cant convert gameobject to transform. I have declared 'Arrow' at the top but I don't believe its that, any ideas? Thanks
Did you follow this example (in C#) to understand how works Instantiate function ?
http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html
I think the answer to your mistake is here :) Look in the second example. And you'll probably need to cast your instantiate I think to anticipate another Unity Error ;)
$$anonymous$$y instantiating was fine, that has been working for a while, and I did think about the typecasting so I did try it. I type cast as a Transform (I tried GameObject and RigidBody too just in case) but it doesn't mitigate the error.
Sorry I forget you cast it :/ Probably too tired and I didn't see the end of your line :x. So for your error now : currentTarget and target get the same type ? Transform & Transform OR GameObject & GameObject ?
both currentTarget and Target are GameObject variables, the error message says the line thats wrong is this:
arrow = Instantiate(projectile, bow.transform.position, bow.transform.rotation) as GameObject;
The error is still the Unable to convert gameobject to transform message. Would it be helpful for me to send you the relevant scripts? thanks.
Answer by Loius · Nov 14, 2012 at 07:43 PM
"Expression denotes a 'type' where a 'variable' 'value' or 'method group' was expected"
An "Expression" is, basically, anything that can be interpreted as a group. (x+1) is an expression, Arrow is an expression, Instantiate() is an expression.
One of your expressions is a type (meaning, when the compiler sees it, it gets an actual class back), but it should be a variable, a value, or a method group (function call).
Just noticed - it looks like you're mixing JS and C# syntax. C# declares variables as "Type name = (Type)InitializationFunction()"; JS declares them as "var name : Type = InitializationFunction() as Type". Pick the format that fits your file type :P
// this is valid but not in this case V
You can't name variables the same thing as classes. That's a minor reason why everyone will recommend that you use CamelCase for classes and functions, and camelCase for variable names.
Change your variable name from Arrow to something that isn't a class name.
Great thanks, I did end up changing it but I didn't have and classes, functions or scripts named 'Arrow' so in this instance it shouldn't have been affecting it
Just noticed - it looks like you're mixing JS and C# syntax. C# declares variables as "Type name = (Type)InitializationFunction()"; JS declares them as "var name : Type = InitializationFunction() as Type". Pick the format that fits your file type :P
you can put 'as Type' in C#, it works and I'm using it successfully in other scripts :)
He just wants to say it's a jJavascript(Unityscript) logic not a C# logic. It's just that.
Huh, I've never had luck using As in C#. I must be terrible at the typing. :)
I'm out of ideas, though - I hope the other answerer can get you to a solution.