- Home /
Grab a variable from each planet in scene and put it into an array
Hi, I have a scene full of planets, and for something else I need to grab the variable "force" from a script called "Gravity2" attached to each of them and put those variables into an array called "Forces". I have tried a lot of things and I've just ended up confusing myself. The planets are the only non-GUI thing in the scene, and by planets I am talking about all objects with the tags "RedPlanet" "BluePlanet" "RedSun" "BlueSun" and "BlackHole". Just some ideas would be very helpful, thanks! Here is the script.
#pragma strict
function Start () {
}
function Update () {
}
static function CreatePath(a : float , v : float, x : float, obj : GameObject) : Vector3[]{
var points : Vector3[] = new Vector3[4];
//Creates Line
for(var i : int = 0; i < 3; i++){
var val : int = i + 1;
points[i] = Vector3(val * v * Mathf.Cos(a), val * v * Mathf.Sin(a), 0);
if(x > 0){
points[i].x *= -1;
}
}
for(var current in points){
current += obj.transform.position;
}
//End Create Line
//Add Net Force
Forces(obj);
//End Add Net Force
return points;
}
static function Forces(obj : GameObject){
var planets : Array = GameObject.FindGameObjectsWithTag("BluePlanet");
planets.Add(GameObject.FindGameObjectsWithTag("RedPlanet"));
planets.Add(GameObject.FindGameObjectsWithTag("BlueSun"));
planets.Add(GameObject.FindGameObjectsWithTag("RedSun"));
planets.Add(GameObject.FindGameObjectsWithTag("BlackHole"));
var forces : Array;
if(planets.length > 0){
for(var planet in planets){
var planetGravity : Gravity2 = planet.gameObject.GetComponent("Gravity2");
var force : Vector2 = planetGravity.force;
forces.Add(force);
}
}
}
What about making the force var in the gravity script public so it shows up in the inspector, that way you can probably take it from the planet object itself I believe
The main problem I have right now is referencing the planet inside my for loop at lines 48-53 because the error says "gameObject is not a member of Object". This happens because the planets array is a Javascript array ins$$anonymous$$d of the built in arrays so when I reference planet it sees planet as an Object and not a GameObject.