Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by MithosAnnar · Feb 22, 2012 at 03:49 PM · javascripterrorgameobjectarray#pragma strict

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.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Feb 23, 2012 at 04:32 PM 1
Share

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.

avatar image MithosAnnar · Feb 23, 2012 at 06:18 PM 0
Share

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

avatar image Jessy · Feb 23, 2012 at 07:01 PM 0
Share

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)

$$anonymous$$ost 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.

avatar image Jessy · Feb 24, 2012 at 12:38 PM 0
Share

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.

avatar image MithosAnnar · Mar 06, 2012 at 03:32 PM 0
Share

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. $$anonymous$$y question was not at all, put across well and for that I am sorry.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by MithosAnnar · Mar 06, 2012 at 03:33 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Jessy · Feb 22, 2012 at 05:41 PM

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jessy · Feb 23, 2012 at 01:05 PM 0
Share

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

avatar image MithosAnnar · Feb 23, 2012 at 01:13 PM 0
Share

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

i.Send$$anonymous$$essage ("DragAndDrop");

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

avatar image Jessy · Feb 23, 2012 at 02:28 PM 0
Share

I never saw anything about Send$$anonymous$$essage (which I do not recommend using).

avatar image Eric5h5 · Feb 23, 2012 at 04:39 PM 1
Share

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 ins$$anonymous$$d 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....)

avatar image Jessy · Feb 23, 2012 at 06:12 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Access a String array in one script from another script 0 Answers

IndexOutOfRangeExeption - Array index is out of range 2 Answers

Unity cannot find component of an object in an array. 2 Answers

exspecting EOF, found '}'. 3 Answers

NullReferenceException with object array 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges