x


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.

more ▼

asked Nov 14, 2012 at 12:05 AM

venhip gravatar image

venhip
169 38 938 47

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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 ;)

more ▼

answered Nov 14, 2012 at 12:16 AM

liszto gravatar image

liszto
2.2k 92 78 89

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

Nov 14, 2012 at 01:20 AM venhip

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 ;)

Nov 14, 2012 at 06:27 AM liszto

My 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.

Nov 14, 2012 at 07:24 AM venhip

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 ?

Nov 14, 2012 at 08:29 AM liszto

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.

Nov 14, 2012 at 04:12 PM venhip
(comments are locked)
10|3000 characters needed characters left

"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.

more ▼

answered Nov 14, 2012 at 07:43 PM

Loius gravatar image

Loius
14.8k 34 47 89

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

Nov 14, 2012 at 08:13 PM venhip

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

Nov 14, 2012 at 10:23 PM Loius

you can put 'as Type' in C#, it works and I'm using it successfully in other scripts :)

Nov 14, 2012 at 10:32 PM venhip

He just wants to say it's a jJavascript(Unityscript) logic not a C# logic. It's just that.

Nov 14, 2012 at 11:40 PM liszto

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.

Nov 15, 2012 at 06:24 PM Loius
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3555
x1359
x210
x185
x2

asked: Nov 14, 2012 at 12:05 AM

Seen: 1774 times

Last Updated: Nov 17, 2012 at 02:54 AM