Notifications
Clear all

[Closed] Stuck Parenting Objects

Hi all,

I’m really confused about the error I’m getting

I have an array of objects LinkArray. I am using a for loop to link these objects together

So if i go

for g = 1 to LinkArray.count do
		(
		
		LinkArray[g].parent = LinkArray[g+1]
		
		)


Everything works great, but if i wanted to change the hierachy and do something like this

for g = 1 to LinkArray.count do
		(
		
		LinkArray[g+1].parent = LinkArray[g]
		
		)


I get an error saying it can’t find the property parent?!!

Any help appreciated.

5 Replies
for g = 1 to LinkArray.count - 1 do
(     
	 LinkArray[g+1].parent = LinkArray[g] 
)

Ahh, thanks, is that because of the 0 start in array?

No, arrays in max start with 1.

At the last iteration you’re trying to access an element past the end of the array: LinkArray[LinkArray.count+1].parent

In the first example you may not get an error, but the last element’s parent will be set to undefined. That’s because if you have an array of size 10, the 11th (and past) element of the array is undefined.

no… your first example

for g = 1 to LinkArray.count do     LinkArray[g].parent = LinkArray[g+1]

works because on the last iteration ( when g == LinkArray.count) then indexing past the end of LinkArray with g+1 it returns undefined which is a valid state for the parent attribute. In your second example the same condition exists in reverse but unfortunately parent is not a valid attribute of undefined !

Ahhhh I get it now! Thanks for the explanation guys! Yeah I was only looking at the first calculation!