- Home /
how do I access these game objects by number?
I am making this thing that could have a lot of objects. Instead of doing a bunch of variables I wanted to try to do it a more efficient way. So this is the var.
here with the []
var object : GameObject[];
function Update()
{
//how do i know how many objects i have
//how do i access them by a number
}
plz answer in javascript
Answer by iamthecoolguy11 · Oct 23, 2013 at 12:51 AM
i ended up useing
var object : GameObject[];
function Update()
{
//put the number in that you want to use
Destroy(object[0]);
object[1].transform.Translate(Vector3.forward * Time.deltaTime)
}
Answer by darthbator · Oct 22, 2013 at 01:48 AM
First off do not code this in Update. I would simply update the "count" of objects at a destruction or instantiation event. that's when objects are joining or leaving the scene. There's no need to evaluate them every single frame.
The best way is to get all of those objects into a List (kinda like you are showing). I would not use the built in .net array as you are showing. You're most likely going to want to look at generic lists (there are available in JS as well). Then as long as you are maintaining the list (adding new objects and removing old ones) you can always just get it's count field to see how many objects are active. You can use List.Find in order to find objects contained within your list.
can you show List.Find in a simple script like the one I made.
Your answer
![](https://koobas.hobune.stream/wayback/20220613120408im_/https://answers.unity.com/themes/thub/images/avi.jpg)