- Home /
problems accessing variables and functions in another script
I have two scripts main and joints.
I want to access functions and variables in joints from main.
I have a gameObject named MainObject which both scripts are attached to.
This is not working so far.
joints:
var myarray=new Array(); myarray[0]="test";
main:
var myjoints:joints; myjoints=GameObject.Find('MainObject').GetComponent(joints); Debug.Log(myjoints);//returns MainObject (joints); Debug.Log(myjoints.myarray[0]);//returns out of index.It seems like it finds the joints script but not any variables attached to the script. Can anyone explain why this is not working?
thanks,
Dan
It doesn't seem to make any difference how I reference the gameobject. It still gives the same result. It finds the joints script --or so it says, but can't find the variable in the joints script.
It works fine if I add yield WaitForSeconds(5); before checking the array. So it's some ti$$anonymous$$g issue, but what?
Answer by Tasarran · Oct 05, 2011 at 09:37 PM
This has been answered before, but here goes again...
(BTW, please use code tags to define your code, it's the button with the 1's and 0's on it)
Your MAIN should get the connection to JOINTS this way:
var myjoints;
myjoints=gameObject.GetComponent(joints);
GameObject (with capital G is different than gameObject. Lowercase g means the gameobject this script is attached to. Only use capital G to refer to the GameObject type. Also, there was no need to do the Find(), just use GetComponent. You only need to do the Find when you are looking outside the current GameObject...
I would suggest making the types explicit for js code: var myjoints : joints = gameObject.GetComponent(typeof(joints)) as joints;
And if myjoints.myarray[0] is returning out of index, then that simply means your array is uninitialized. So you need to make sure that your first script is testing the value of myarray only after it has been set to something.
@Steven Walker is right: maybe main is running before the initialization of myarray. You can assign the value to myarray in joints' Awake, and read it in main's Start or Update.
I often get around this by adding a variable to script like: var isSetup : boolean = false;
function Start() {
// do intialization stuff
isSetup = true; }
Then in your other script you can check:
while(!myjoints.isSetup) yield;
This will force main script to wait until the other is ready. NOTE: You will likely need to do the yield in another method such as:
StartCoroutine("DoSetup")
I need to learn more about what code loads first since there are two scripts attached to the same object. Is there a way to force the order to be correct. If I use Awake() in one and Start() in the other how will I know which one fires first. I guess I can test with debugs???
Your answer
Follow this Question
Related Questions
Accessing function from another script won't work 1 Answer
Get GameObject name from other object. 2 Answers
Accessing classes class from other script 2 Answers
Access others functions C# 2 Answers
Access other script from Editor script 2 Answers