i want to be able to get all gameobjects with the name (“cube”) from my scene and add it to an array.
not sure how
i’ve tried
var cube : GameObject;
@Update
cube.add(GameObject.Find(“cube”));
print(cube.Length);// to see if it worked
i want to be able to get all gameobjects with the name (“cube”) from my scene and add it to an array.
not sure how
i’ve tried
var cube : GameObject;
@Update
cube.add(GameObject.Find(“cube”));
print(cube.Length);// to see if it worked
I just finished working on something like this but I used TAGS to get all GameObjects of a certain type in an array. So in your case you would tag each game object you want under Cube, then do something like:
var cubes : GameObject[]; // Define variable cubes as a built in array of GameObjects
cubes = GameObject.FindGameObjectsWithTag("Cube"); // Find all game objects with the TAG "Cube". Note, you must actually create the tag and attach it to the game object for it to work.
Then you can do what you want with the results. For example, I wanted to get the names of each game object with the tag (in this case “Cubes”) and push it into another array, so I did something like:
var cubeNames = new Array(); // I used a new array because you can't push values into a builtInArray[];
for (var value : GameObject in cubes) { //Loop though all game objects found in cubes...
cubeNames.Push(value.name); // and push the "name" into the cubeNames array.
//Debug.Log(value.name); // Display results in the Debug.
}
Hope this helped.
try adding manually via inspector
thanks for replies, found my answer here, but i am still stuck on how to make the enemy move onto the next closest waypoint (object) after it’s been to one. (currently its just gets stuck to one)
function Update () {
var waypoints: GameObject[] = GameObject.FindGameObjectsWithTag("waypoint");
var closest: GameObject;
var closestDist = Mathf.Infinity;
for (waypoint in waypoints) {
var dist = (transform.position - waypoint.transform.position).sqrMagnitude;
if (dist < closestDist) {
closestDist = dist;
closest = waypoint;
}
}
transform.LookAt(closest.transform);
}
You should post this as a new question - nobody will find your question here, since it's classified as an answer!
– aldonaletto