- Home /
Store Rigidbodies in Array
Hi, I am storing the rigidbodies of three gameobjects in three variables.
var ki1 : Rigidbody;
var ki2 : Rigidbody;
var ki3 : Rigidbody;
I assigned them on the editor. After that, I stored the variable names in an Array (ki1, ki2, ki3).
I shuffel them and go through that array. But when I try to put an Addforce to them (like: array[i].AddForce(3000.0, 0.0f, 0.0f);) unity tells me "'AddForce' is not a member of 'Object'".
I do understand the problem as "ki1, ki2 and ki3" are just strings. How do I workaround that? I am not really sure how to achieve that. Isn't it possible to just store the variables in an array and call its items by its variable name?
Thank you
Sounds like you're using JS Arrays; don't ever do that. Only use built-in arrays or generic Lists. But as instruct9r says, you can bypass all this just by using a built-in array directly in the first place. When using arrays, there's no reason to use separate variables at all.
Answer by instruct9r · Apr 14, 2015 at 11:26 PM
mm.. Why don't you just make
var kiRBs : RigidBody[]
which will make array, for rigidBodies and then use that to loop through it and AddForce...
You don't show how do you assign these rigidbodies in the array, so we can't help you otherwise. Maybe it'll be better to show your whole code so we can see where is the problem...