Search Unity

Getting data from one script to another?

Discussion in 'Scripting' started by Morgan, Jun 10, 2006.

  1. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    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 :) 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!
     
  2. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    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):
    Code (csharp):
    1.  
    2. GameObject myObject = GameObject.Find("myObject's name");
    3.  
    4. myObject.transform.position = whatever;
    5.  
    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 ;-)

    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
     
  3. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    There are at least 2 ways.

    1. Have a reference to the object that you want to access.
    Code (csharp):
    1.  
    2. // At top of script, theVariableName can be anything you want to use to
    3. // refer to the script in you code. This declaration is the key
    4. var whatYouWantToCallTheScript : YourScriptType;
    5. ....
    6.  
    7. // In any old function
    8. function AFunctionThatDoesSomething () {
    9.  
    10.   // To reference a variable directly
    11.   if (whatYouWantToCallTheScript.theValueThatYouAreInterestedIn == 5.0) {
    12.      // To call a function instead of referencing a variable
    13.      whatYouWantToCallTheScript.TheFunctionYouWantToCall ();
    14.   }
    15. }
    16.  
    2. 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
    Code (csharp):
    1.  
    2. var whatYouWantToCallTheScript : ScriptName;
    3. // Establish the reference, I am using Start() because I only want to set up the reference once.
    4. // You can do this each time if you don't want to declare the variable at the class level
    5. function Start () {
    6.   myObject = GameObject.Find("myObject's name");
    7.  
    8.   // To get a specific script attached to the object
    9.   whatYouWantToCallTheScript = myObject.GetComponent(ScriptName);
    10. }
    11.  
    You can get the object's name by selecting the object and looking in the Inspector.
     
  4. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    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.
     
  5. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    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?)
     
  6. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    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.
    Code (csharp):
    1.  
    2. // To declare a variable of the type of your other script
    3. var yourVarName : TheNameInTheInspectorOfTheScriptYouWantToReference;
    4.  
    5. // To Reference a variable in the other script
    6. anotherVariable = yourVarName.aVariableInTheReferencedScript;
    7.  
    8. // To Call a function in the other script
    9. yourVarName.AFunctionName();
    10.  
     
  7. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    Many thanks--got it!