- Home /
Problem is not reproducible or outdated
Access variables, objects in array in another script?
Sorry if the title seems confusing but I've either really overcomplicated things or i'm just doing something very stupid....anyway, here goes.
Basically what i'm trying to do is plot where a planet is in the solar system by ways of using it's semi major axis, mass etc. Now these values are in one script called "Planet" which contains the values
name, mass, semiMajorAxis, inclination, avgSpeed
with functions to return these named Getmass(), GetsemiMajorAxis() etc etc
In another script I have named "Kepler Coords", I have an array called "planet", now this array contains prefab spheres with teh "Planet" script attached to them with the values filled in, In the "Kepler Coord" script I need to be able to access the variables of the "Planet" script, however try as I may I am getting no joy.
here is part of the "Kepler Coord" script,
var planet : GameObject[];
var planetScript : Planet;
function Awake ()
{
var planet = GameObject.FindGameObjectsWithTag("planet");
}
private function BuildSystem ()
{
bodies = new GameObject[(planet.length -1)];
for (var i : int = 0; i<planet.length; i++)
{
var tMass : float = mass * massConversion;
mass += tMass;
var tRadialDistance : float = semiMajorAxis * distanceConversion;
var tInclinationAngle : float = inclination * ((2.0*Mathf.PI) / 360);
var tPositionX : float = tRadialDistance * Mathf.Cos (tInclinationAngle);
var tPositionY : float = tRadialDistance * Mathf.Sin (tInclinationAngle);
var tPosition : Vector3 = new Vector3 (tPositionX, 0.0, tPositionY);
var tVelocityZ : float = avgSpeed * (distanceConversion/timeConversion);
var tVelocity : Vector3 = new Vector3 (0.0, tVelocityZ, 0.0);
var tBody : GameObject = Instantiate(planet, tPosition, Quaternion.identity);
tBody.rigidbody.mass = tMass;
tBody.rigidbody.velocity = tVelocity;
As you can see I need to access the variables from the "Planet" script, but whenever i try planet.GetComponent("mass") or planet.GetComponent(GetMass()) or planet.GetComponents("mass") etc I keep getting an error saying that "GetComponent is not part of GameObject[]".
Any help would be much appreciated as I'd rather not go down the XML route since I have no understanding of it.
Answer by flaviusxvii · Sep 27, 2012 at 04:42 PM
GetComponent() is to be used with the name of the script/class (the fact this is the classname is hidden from you in Unityscript), not the methods or members of the class.
So you'd do something like `var kc : KeplerCoord = planet.GetComponent("KeplerCoord");` and then get the mass or whatever from the variable kc.
Answer by Supershandy · Sep 30, 2012 at 06:47 AM
I think i had just massively overcomplicated thngs as it said it could not access variables since they weren't static, so I change them all to static which just changed all the values to zero (Since I had put all the values in through the inspector).
I think for this project a locally-acccessed XML is going to be the better option since there are going to be a lot of variables floating around and i'd rather try and keep them all in one place and keep the script levels down as much as possible.
Thnak you though for the help, it was much appreciated :)