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
0
Question by Jason-H · Apr 09, 2011 at 08:36 AM · loopfor-loop

What's wrong with this for loop?

Hi, I'm unsure as to how I can write the following as a for loop. Basically this code works perfectly for 3 items, but I want to be able to choose how many items there are on each level which does all of this automatically instead of me having to hard code the number of items each time.

var item0 : GameObject; var item1 : GameObject; var item2 : GameObject;

var ScriptOnItem0 : DynamicListOfItemsOnItem; var ScriptOnItem1 : DynamicListOfItemsOnItem; var ScriptOnItem2 : DynamicListOfItemsOnItem;

function Start() { ScriptOnItem0 = item0.GetComponent (DynamicListOfItemsOnItem); ScriptOnItem1 = item1.GetComponent (DynamicListOfItemsOnItem); ScriptOnItem2 = item2.GetComponent (DynamicListOfItemsOnItem); }

function OnGUI() { if (listButtonPressed) { ScriptOnItem0.ShowTextures(true); ScriptOnItem1.ShowTextures(true); ScriptOnItem2.ShowTextures(true); } }

The Best I've come up with so far is;

var itemsArray : GameObject[];

var ScriptsOnItemsArray : DynamicListOfItemsOnItem[];

function Start() { for (var index : GameObject in itemsArray) { ScriptsOnItemsArray[index] = itemsArray[index].GetComponent (DynamicListOfItemsOnItem); } }

function OnGUI() { if (listButtonPressed) { for (var index : GameObject in itemsArray) { ScriptsOnItemsArray[index].ShowTextures(true); } } }

In the start function I'm trying to link the 1st array item of the script on itemsArray with the 1st variable of ScriptsOnItemsArray.

The error in Unity comes out as saying "Cannot convert 'UnityEngine.GameObject' to 'int' on the line that reads;

ScriptsOnItemsArray[index] = itemsArray[index].GetComponent (DynamicListOfItemsOnItem);

What am I doing wrong?

Thanks!

Comment
Add comment
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

1 Reply

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

Answer by efge · Apr 09, 2011 at 08:53 AM

The index of an array is an Int so your for loop could look like this:

for (var index : int; index < itemsArray.Length; index++)
{
  ScriptsOnItemsArray[index].ShowTextures(true);
}
Comment
Add comment · Show 3 · 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 Jason-H · Apr 09, 2011 at 09:08 AM 0
Share

Thanks a lot for your answer, it works! But why did my version not work? Is the way I wrote my for loop not the correct way?

avatar image efge · Apr 09, 2011 at 09:18 AM 0
Share

You used a GameObject ins$$anonymous$$d of an Int variable to assign an element of the array.

avatar image Jason-H · Apr 09, 2011 at 09:53 AM 0
Share

Ah ok, thanks a lot.

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

No one has followed this question yet.

Related Questions

Getting an error with my for/in loop! 3 Answers

Rearranging pre-existing objcets into a grid 2 Answers

using "for" in Unity Script HELP! 2 Answers

How to have a (wait) yield on a for loop? 0 Answers

How do I reset a for loop variable??? 3 Answers


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