I’d like to jump in on this thread as well, as I’m having some trouble with inter-Script communication as well.
To jump in and expand on some of the answers for Akabane:
If you look at the tutorials, there are several similar solutions to your problem, and all of them are related to caching a link between the scripts.
The two methods described above go as follows: (Here I will use the example of looking for a variable in a script called LevelStatus from a script called PlayerStatus.)
Make a variable to an object that you set in the inspector:
PlayerStatus.js
var levelStatus : LevelStatus;
function Start () {
if (levelStatus == null)
Debug.Log("Player Status Start: No link to Level Status");
Here you’ve set up an exposed variable that will appear in the inspector, and you can drop the gameObject that contains your LevelStatus script on it.
In this case, your Level Attributes gameObject contains the script LevelStatus. Drag the gameObject Level Attribute onto the empty variable slot in Player Status. This will set the link.
In this case, the function Start() and Debug.Log are there to make sure you’ve not made an error.
The advantage to this method is that you do not need to know what you are looking for at the time you are writing the script - you’ve simply made a place holder. Another advantage is that if you change your choice of components, you can simply drag a new object onto that variable slot.
The drawback to this method, is there are ways that you can break this connection and the PlayerStatus.levelStatus resets to “null” or nothing, empty, etc… At this point you must reattach the script by hand.
The other method is contained completely within the code of the scripts.
PlayerStatus.js
private var levelStatus : LevelStatus;
function Start () {
levelStatus = FindObjectOfType(LevelStatus);
if (levelStatus == null)
Debug.Log("Player Status Start: No link to Level Status");
In this case, you are setting a PRIVATE variable - it will not be expose in the inspector - and you are actually using the function Start() to do something. You are asking it to go and find the object you want while initializing the script, and caching it without your help.
The drawback to this method, is that you must know what you are looking for at the time you are writing your script, and if the component you want changes, you will have to re-write your code.