x


for loop error

In my Inventory script (with #pragma strict turned on) I Get the error Cannot convert 'Object' to 'UnityEngine.GameObject'.

var items : List.;

function OnGUI () {

for (var item : GameObject in items) { //error here I have gone everywhere but no one seems to have encountered this.

more ▼

asked Feb 22 '12 at 03:49 PM

MithosAnnar gravatar image

MithosAnnar
299 22 36 40

The code you posted will compile and run without any errors. The reason no one has encountered any problem with this is because there isn't any. I would guess you actually have different code that's creating a problem.

Feb 23 '12 at 04:32 PM Eric5h5

I'll update the question now, with a more helpful list of problematic and complicated code =D

Feb 23 '12 at 06:18 PM MithosAnnar

You need to listen to the errors you get; your code generates plenty of errors that aren't the one you gave us:

Take out the parentheses after var items : List.

items.Add(LOWERCASE I HERE)

Most importantly, you didn't define DropAndDrag.

After those things are taken care of, there are no longer any errors. This code is useless to us, and so doesn't help you get your question answered. You need to spend some time learning how to cut down on what is irrelevant, so you can move forward.

Feb 23 '12 at 07:01 PM Jessy

Please try to understand what you are saying. You post us snippets that don't compile, and don't yield the error you mentioned. That is useless.

Feb 24 '12 at 12:38 PM Jessy

when I did what you guys said to do with the for loop, it didn't work. And then I got the error for sending a message. My question was not at all, put across well and for that I am sorry.

Mar 06 '12 at 03:32 PM MithosAnnar
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

The correct and simple answer is this:

for (var item in items) {

   (item as GameObject).SendMessage("Activate");

}

I decided to come back to this after a while. And it took me an hour and a half but this is what works. Jessy and Eric5h5, thank you both so much for helping me to get the correct answer.

more ▼

answered Mar 06 '12 at 03:33 PM

MithosAnnar gravatar image

MithosAnnar
299 22 36 40

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

http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponent.html

http://unity3d.com/support/documentation/Manual/Generic%20Functions.html

var i = item.GetComponent.<Item>();

which is a shorthand for

var i = item.GetComponent(Item) as Item;

There is tons of information on this on Unity Answers and the forum.

more ▼

answered Feb 22 '12 at 05:41 PM

Jessy gravatar image

Jessy
15.6k 72 95 196

I have no idea what you just said! :-\

Feb 23 '12 at 01:05 PM Jessy

I already fixed the item.GetComponent problem I was having, with:

i.SendMessage ("DragAndDrop");

But I still can't figure out the for loop error.

Feb 23 '12 at 01:13 PM MithosAnnar

I never saw anything about SendMessage (which I do not recommend using).

Feb 23 '12 at 02:28 PM Jessy

In Unityscript you should use var i = item.GetComponent(Item);. It's faster than the generic version, and less ugly. There's no reason to use var i = item.GetComponent(Item) as Item;. The only reason you'd use casting is if you were using a string in GetComponent, so that it was forced to return Component instead of the type. (No, the docs still have not been updated with the correct info, even though this has been the case since Unity 3.4....)

Feb 23 '12 at 04:39 PM Eric5h5

Eric, you ought to make that an answer; you know UnityScript better than anybody, I think.

Feb 23 '12 at 06:12 PM Jessy
(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:

x3446
x2077
x1943
x1356
x4

asked: Feb 22 '12 at 03:49 PM

Seen: 712 times

Last Updated: Mar 06 '12 at 03:33 PM