ok i have gone through the API and I have used this:
// To access public variables and functions
// in another script attached to the same game object.
// (ScriptName is the name of the javascript file)
var other : ScriptName;
other = gameObject.GetComponent("ScriptName");
// Call the function DoSomething on the script
other.DoSomething ();
// set another variable in the other script instance
other.someVariable = 5;
But what im trying to do is access an int variable to find a certain segment of my character and then add its position to an array… But its throwing me up errors, am i missing something?
I have copied and pasted what part of the code is relevant.
//List that holds the segments
var segmentList = new Array();
//Accessing the script
var other : SegmentScript;
other = gameObject.GetComponent("SegmentScript");
//on startup
function Start () {
segmentList.Push(this.transform.position);
segmentList.Push(GameObject.Find("body1").transform.position);
segmentList.Push(GameObject.Find("body2").transform.position);
var temp : GameObject;
temp = GameObject.Find("body1");
temp.GetComponent(other.segmentIndex);
segmentList.push(temp.transform.position);
var body2 : GameObject;
body2 = GameObject.Find("body1");
body2 = GetComponent(other.segmentIndex);
segmentList.push(body2.transform.position);
}
That is because it is wrong lol. That is a typo, that should of been body2.GetComponent(other.segmentIndex);
But that gives the same error. Im trying to get access to the “segmentIndex” variable from another script to use it as part of a selection process. But not to change that variable.
Basically im trying to give each segment in a list an index number, so a new segment can check the index number of the previous segment and can update to that segments last position.
Line 6 is wrong. You can’t do assignments like that outside a method. Declare other outside - give it a value in Start or Awake.
Line 6 again - don’t use the string overload of GetComponent. It’s slower and you have to cast the result to the correct type before using it anyway.
temp and body2 are declared as GameObject. GetComponent does not return a GameObject.
GetComponent has no overload for int so GetComponent(other.segmentIndex) won’t work. (Assuming segmentIndex is an int, which given the name, it seems to be.)
I thought that it would not matter, considering I have already declared temp and body2 to a gameobject and found that game object in the next line, then got the component of said object in the trouble line. Or does that not work that way?
EDIT: As it turns out I actually want to be setting the variable from the other script in this script, and not accessing it for other purposes.