- Home /
Accessing Vector3s of GameObjects in Arrays (JS)
Hello,
I'm having trouble accessing the transform and rotational data of nodes stored in a Javascript array. I either can't find the proper syntax, or am approaching the problem incorrectly.
This script finds all of the camera nodes (by tag 'Camera Node') and stores them in an array. It then attempts to access the transforms of those GameObjects so that it can move the camera to them.
My code (JS): // VARIABLES FOR CAMERA NODES var cameraNodes = new Array (); var currentCamera : int = 0; var nextCamera : int;
function Start () {
// Find Nodes
cameraNodes = GameObject.FindGameObjectsWithTag("CameraPos");
}
function MoveNext () {
nextCamera = (currentCamera + 1);
// Determine if nextCamera is bigger than array
if (nextCamera > cameraNodes.length) {
nextCamera = 0;
}
Debug.Log("I am going to cameraPosition " + (nextCamera + 1));
iTween.MoveTo(gameObject,(cameraNodes[nextCamera].Vector3),2);
currentCamera = nextCamera;
}
Answer by robertbu · Nov 27, 2013 at 02:41 AM
A few changes here. First, declare 'cameraNodes' as:
var cameraNodes : GameObject[];
You don't need to initialize an array for 'cameraNodes' since FindGameObjectsWithTag() returns an array. Plus if you need a collection for a list of game objects, use the .NET generic List class rather than the Array class.
Then on line 21, you can access the nodes as 'cameraNodes[nextCamera].transform.position'
iTween.MoveTo(gameObject,cameraNodes[nextCamera].transform.position, 2.0);
Your answer
Follow this Question
Related Questions
How do I make an array of vectors? 1 Answer
How do you get iTween to properly implement PutOnPath command? 1 Answer
Avoiding instantiating two things in same location? 1 Answer
Javascript - find a value in an array 0 Answers
Array.Push() for Vector3[] or how to add items to Vector3 array without knowing index 1 Answer