x


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!

more ▼

asked Apr 09 '11 at 08:36 AM

Jason H gravatar image

Jason H
200 30 31 42

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

1 answer: sort voted first

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);
}
more ▼

answered Apr 09 '11 at 08:53 AM

efge gravatar image

efge
5.1k 5 14 38

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?

Apr 09 '11 at 09:08 AM Jason H

You used a GameObject instead of an Int variable to assign an element of the array.

Apr 09 '11 at 09:18 AM efge

Ah ok, thanks a lot.

Apr 09 '11 at 09:53 AM Jason H
(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:

x294
x95

asked: Apr 09 '11 at 08:36 AM

Seen: 707 times

Last Updated: Apr 09 '11 at 08:36 AM