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
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.
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)
}