now I’m staring at the monitor because I can’t pass a Matrix to a script throug an exposed variable.
This is my situation:
Script-1 needs to pass to Script-2 a Matrix made of different data type (int, string, Vector4).
Script-1
// I create the Matrix
var matrix = new Array()
matrix[0] = new Array()
...
and so on
// I send the matrix to Script-2
object.GetComponent(Script-2).variable = matrix
Script-2
var variable;
function Start(){
Debug.Log(variable);
}
The result is: NullReferenceException: Object reference not set to an instance of an object
Have you verified that “object.GetComponent(Script-2)” is returning what you think it’s returning? If that is failing, that might be the null reference that it’s talking about.
Yes, I’ve checked everything 'till the operation of assignment.
I get the correct script and the correct variable (I tried to pass a vector to the same variable and it goes right)
First I assume that the names Script-1 and Script-2 are just examples and not the actual script names you are using, as script and variable names are not allowed to contain the - character.
In order to reduce confusion, I will call them Script_1 and Script_2 in my examples.
You are accessing the variable inside Start(). The problem is that there is no guarantee that the initialization code in Script_1 will be run before the code inside the Start function of Script_2.
To ensure that the initialization has completed, change Script_1:
// I create the Matrix
var matrix : Array;
function Awake() {
matrix = new Array()
matrix[0] = new Array()
...
and so on
// I send the matrix to Script_2
var s2=object.GetComponent(Script_2);
if( s2 )
s2.variable = matrix;
else
Debug.Log("Error: object has no Script_2 component attached",object);
}
Awake is guaranteed to have been executed before any Start functions are called, so the initialization in Script_1 will complete before Script_2 tries to use the variable.
I’m not sure that I understand. That was my solution: Putting the initialization of the exposed variable into an Awake function will remove the Null reference exception you reported.
As Keli indicated it’s not so easy to separate the issues in this case. If both of your scripts execute code on Start() then you have no guarantee that Script-1 has set the variable in Script-2 before you try accessing it. However, if you switch Script-1 so that it initializes the matrix and sends it off to Script-2 on Awake() then you can guarantee that the variable is ready by the time Script-2’s Start() function is called:
Awake()
Script-1 inits the matrix and passes it to Script-2
Start()
Script-2 accesses its variable
If instead you use Start() for both scripts it might be:
Start()
Script-1 inits the matrix and passes it to Script-2
Script-2 accesses its variable
Or it could be:
Start()
Script-2 attempts to access the variable and throws and error as Script-1’s Start() function hasn’t executed
There are no general issues with the notion of passing an array of mixed data types so it seems this may all be an order of operations issue which is what Keli was trying to say (as you cannot guarantee the order in which Start() functions will be triggered). Have you given his solution a try? If so did it help?
Here are two scripts that work just fine on my end:
Script-1:
var Blah : Array;
function Awake() {
Blah = new Array();
Blah[0] = new Array();
Blah[1] = 5;
Blah[2] = Vector4(0,0,0,1);
var s2= GetComponent("Script-2");
if( s2 ) {
s2.Foo = Blah;
} else {
Debug.Log("Error: object has no Script_2 component attached");
}
}
Script-2:
var Foo : Array;
function Start() {
Debug.Log(Foo);
}