Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Parameter passing over different scripts and objects

Discussion in 'Scripting' started by slumberus, May 4, 2007.

  1. slumberus

    slumberus

    Joined:
    Apr 17, 2007
    Posts:
    39
    Hey guys I've got another problem,

    script A has a local var p : transform;

    script B wants to dynamically alter script A's p variable (determine what type of prefab p uses). How to I pass the parameter from script B to script A?

    script A and script B are in different game objects. (script B is in a UI button, script A is in an object that spawns p)

    thx.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You can use GetComponent to get an object's script, and alter that script's variables. A shortcut is to define the object by using the script's name as a type instead of GameObject (or Rigidbody or whatever). For example, if you use this as "script B" on the UI button:

    Script B:

    Code (csharp):
    1.  
    2. var otherObject : ScriptA;
    3.  
    4. function Start () {
    5.     var otherObjectClone = Instantiate(otherObject);
    6.     otherObjectClone.someVariable = "Remote control!";
    7. }
    8.  
    And say the following script, which we're calling "ScriptA", is attached to the spawned object. (And it literally must be called "ScriptA", unless you change the first line of Script B to use some other name. Otherwise you get an error message about not having a valid type.)

    ScriptA:

    Code (csharp):
    1.  
    2. var someVariable = "default text";
    3.  
    4. function Start () {
    5.     print (someVariable);
    6. }
    7.  
    If that object is instantiated normally, it will print "default text". If, however, it's instantiated by Script B, it will print "Remote control!" Even though you have typed the object by script name instead of GameObject, you can still use transforms and so on...like "otherObjectClone.transform.position = Vector3(20,20,20);" would still work.

    Hopefully that makes sense. ;) If you'd rather use GetComponent, though, the principle is still the same. It's just another line of code. Here's Script B using GetComponent:

    Script B:

    Code (csharp):
    1.  
    2. var otherObject : GameObject;
    3.  
    4. function Start () {
    5.     var otherObjectClone = Instantiate(otherObject);
    6.     var otherObjectScript = otherObjectClone.GetComponent(ScriptA);
    7.     otherObjectScript.someVariable = "Remote control!";
    8. }
    9.  
    --Eric
     
  3. slumberus

    slumberus

    Joined:
    Apr 17, 2007
    Posts:
    39
    Thanks eric! :D Just finished reading the codes.

    The instantiating part might be a bit confusing though. I actually planned to have multiple ScriptA game objects placed across the game world.

    ScriptB in the UI button is actually like a toggle to determine what objects SpawnA gameobjects should spawn.

    Here's the codes for the scripts, maybe it might help clarify what I'm doing (or doing wrong):

    This is scriptA (inside the object spawner):
    Code (csharp):
    1.  
    2. var selection : Transform; // variable to be used by scriptB
    3.  
    4. function OnMouseDown () {
    5.     StartCoroutine ("SpawnIndicator"); 
    6. }  
    7.    
    8. function SpawnIndicator () {
    9. var q = transform.position;
    10. q.y=3;
    11. Instantiate (selection, q, Quaternion.identity);
    12. }
    13.  
    This is scriptB (inside UI Button):
    Code (csharp):
    1.  
    2. var otherObject : ScriptA; // having the object named as ScriptA as well
    3. var newselection : Transform; //new prefab to replace selection variable at scriptA
    4.  
    5. function OnMouseDown () {
    6.    var otherObjectClone = Instantiate(otherObject);
    7.    otherObjectClone.selection = newselection;
    8. }
    9.  
    thanks,
    chris.