Getting data from one script to another?

How do I share data between different objects? Such as:

  • Having the script on one object read the rotation of a different object. Or…

  • Setting a variable in the script of one object, and then allowing another object to read that variabe.

The Unity Tutorial seems to have something like that happening with the Score, but I just get errors when I try to do something similar.

Thanks for bearing with my ignorance :slight_smile: I’m having trouble searching the manual and am probably missing some things that are in there. That, and I’m used to different terminology and probably not asking the right questions!

Unfortunately I can’t help you with the Java script code to do that (I use C#), but you use the GameObject.Find methods (FindGameObjectByTag, name, etc.) and store the returned object in a variable.

This is the C# way to do it (prob wont help you much):

GameObject myObject = GameObject.Find("myObject's name");

myObject.transform.position = whatever;

As for setting variables, there are many ways (static vars, GetComponent(), etc) by I only know the C# syntax for them, so I wont confuse you any more :wink:

Someone with JS experiance can answer, but that is the general way to do it.

If you post your code with the errors we can figure out more what is going wrong.

HTH,
-Jeremy

There are at least 2 ways.

  1. Have a reference to the object that you want to access.
// At top of script, theVariableName can be anything you want to use to
// refer to the script in you code. This declaration is the key
var whatYouWantToCallTheScript : YourScriptType;
....

// In any old function
function AFunctionThatDoesSomething () {

  // To reference a variable directly
  if (whatYouWantToCallTheScript.theValueThatYouAreInterestedIn == 5.0) {
     // To call a function instead of referencing a variable
     whatYouWantToCallTheScript.TheFunctionYouWantToCall ();
  }
}
  1. If you don’t have a direct reference then you can set up the reference at runtime. You might do this if you have some sort of game controller that does not get destroyed between levels. Jeremyace has done the C# for you. Javascript is almost the same
var whatYouWantToCallTheScript : ScriptName;
// Establish the reference, I am using Start() because I only want to set up the reference once.
// You can do this each time if you don't want to declare the variable at the class level
function Start () {
  myObject = GameObject.Find("myObject's name"); 

  // To get a specific script attached to the object 
  whatYouWantToCallTheScript = myObject.GetComponent(ScriptName);
}

You can get the object’s name by selecting the object and looking in the Inspector.

Very helpful–I would not have stumbled across those two methods easily, and I’m sure I’ll use them. Thanks, all! Hopefully in a couple weeks I’ll know enough to deliver some answers and not just questions.

The basic “reading the rotation of another object” turned out to be pretty easy, but for tracking “global” properties like player health, I’ll use one of those two methods.

Now I’ll investigate what the different “script types” might be.

I can’t seem to find script types in the documentation. What are the possible script types, and what type would I use in this case (reading a variable stored by another script)?

(Or better yet, what term should I search for in the scripting docs to teach myself?)

It is the name of the script. If you are using JavaScript then the script is actually a class. If you are using C# or Boo, the script has to be the same name as the class declared in the script.

// To declare a variable of the type of your other script
var yourVarName : TheNameInTheInspectorOfTheScriptYouWantToReference;

// To Reference a variable in the other script
anotherVariable = yourVarName.aVariableInTheReferencedScript;

// To Call a function in the other script
yourVarName.AFunctionName();

Many thanks–got it!