- Home /
Information exchange between scripts
I'm currently working in a game and got a problem with the information exchange between my scripts. That's what i have:
Script A calculates quite much and attached to a emty GameObject. Script B is needed for the movement behaviors of my monsters. Sripts B is attached to each monster in the scene. Script B needs a public variable of Script A. How can i get this information? Script A has get-methods/properties for this, but i don't know how i can call them? Maybe somehow with get_component?
PS: I don't want script B to inherit script A, because script A should only be running once.
Thanks for your help
i have just linked scriptA as variable in Script B. It seems to work fine:
GameObjectLoger gameloger= GameObject.FindGameObjectWithTag("scriptmanager").GetComponent<GameObjectLoger>();
When i try to use the gameloger-methods the variables seem to be emty,although they are not.
public GameObject[]list;
gameloger.Get_Go_list_via_copy(list);
//in GameObjectLoger Sript:
public void Get_Go_list_via_copy(GameObject[] go)
{
for (int i = 0; i < go_list.Length;i++){
go = new GameObject[go_list.Length];
go[i] = go_list[i];
}
}
}
What did i wrong?
Change line 7 to public void Get_Go_list_via_copy(out GameObject[] go)
and the line 3 to gameloger.Get_Go_list_via_copy( out list )
.
The problem is that the reference on the array list
is passed to the function by value so the changes you do inside the function are not affecting the passed object. out
keyword allows function to save the result in the variable. Though it is a better idea just to use the return value because it make the code more readable.
Also add to the line 9 (just before the loop) the following: go = new GameObject[ go_list.Length ]
to create the array itself.
@moonstruck first off, I am not stalking you :) But what you say is wrong. The reference of the array is passed by value but this value points where the array is stored, so you ca modify the original array. What you are doing with using out is that the reference (the address where the array is stored) can now be modified, which is quite dangerous if you do not know what you are doing.
The problem here is that
for (int i = 0; i < go_list.Length;i++){
go = new GameObject[go_list.Length];
go[i] = go_list[i];
}
Create a new array on each iteration and only store one guy. You probably have only the last one populated.
You need your method as such:
public void Get_Go_list_via_copy(GameObject[] go)
{
go = new GameObject[go_list.Length];
for (int i = 0; i < go_list.Length;i++){
go[i] = go_list[i];
}
}
}
This creates two distinct array pointing to the same objects. If you want a shallow copy you simply point one towards the other:
public void Get_Go_list_via_copy(GameObject[] go)
{
go = go_list.Length;
}
Now if you want a deep copy meaning both arrays have their own set of objects. That is go has an independant copy of go_list you need a deep copy which includes copying all members of the object into a new object that is then passed to the array.
Yeah, you are right, I haven't noticed the 10th line. :)
Answer by moonstruck · Jul 11, 2013 at 10:30 AM
Using static variables is ok in some cases but it can cause subtle problems in complex programs. Instead you can add a public variable of type ScriptA
into your ScriptB
and either assign it via the inspector or using any find function (FindObjectsOfType( typeof( ScriptA ) ) as ScriptA
, Find( "ScriptAObjectName" ).GetComponent()
, etc.) if your ScriptB objects are spawned dynamically. Then you can access any public properties, variables, etc. from your ScriptA
using scrptAVariable.someVariable
syntax.
Find( "ScriptAObjectName" ).GetComponent< ScriptA >() thats exactly what i was looking for:) thanks for your help
Answer by zerebruin · Jul 11, 2013 at 09:06 AM
declare a static variable (or function) in Script A
public static var someStaticVar
you can call that variable from any other script (like Script B) by calling scriptname.variable so
ScriptA.someStaticVar
This is a real bad advice for someone that is beginning. The other answer by moonstruck should be prefered as long as you do not understand how static and instance work.
Also, this has many answer or even long explanantio like here: http://unitygems.com/script-interaction1/