x


Create array of children in Start function, use it in Update?

Hey, so what I'd like to do is make an array of children, then move the player to the child in the the array that I choose, heres the code; WayPointContainer is an empty gameobject with empty gameObjects as children.

var WayPoints = new Array();
var WayPointContainer : Transform;
private var CurrentWayPoint : int = 1;

function Start ()
{
    WayPoints.Add(WayPointContainer.GetComponentsInChildren(Transform));
    Debug.Log(WayPoints[CurrentWayPoint].position);
}

function Update ()
{
    if(transform.position.x != WayPoints[CurrentWayPoint].position.x)
    {
        //move
    }
}

the error I get is ; ArgumentOutOfRangeException: Index is less than 0 or more than or equal to the list count. Parameter name: index 1

does this mean it isn't adding the transforms of the children? if so how do I fix this?

more ▼

asked Apr 30 '11 at 09:54 AM

Jason Hamilton gravatar image

Jason Hamilton
470 70 74 80

Remember to use WayPoints.Shift() after adding the children to remove the parent.

Apr 30 '11 at 12:55 PM Joshua
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Array indices start at 0. You have only one item, so to access it you need to use index 0, but CurrentWayPoint is 1 (that is the argument that is out of range).

more ▼

answered Apr 30 '11 at 12:14 PM

Molix gravatar image

Molix
5.2k 21 31 70

But enemywaypointcontainer has 2 children, so the parent is 0 and the two children are 1,2?

May 01 '11 at 05:10 AM Jason Hamilton

Instead of Add(), try AddRange().

May 01 '11 at 12:22 PM Molix

is that something available with javascript arrays?

May 02 '11 at 07:25 AM Jason Hamilton

Yes, they do have it.

May 02 '11 at 02:23 PM Molix
(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:

x5407
x2986
x1888
x591
x223

asked: Apr 30 '11 at 09:54 AM

Seen: 2004 times

Last Updated: Apr 30 '11 at 09:54 AM