Search Unity

Need to get a variable from a collided object.

Discussion in 'Scripting' started by Silverriolu295, Mar 5, 2015.

  1. Silverriolu295

    Silverriolu295

    Joined:
    Nov 12, 2014
    Posts:
    48
    My game is a dungeon crawler. Doors are randomly generated and when you touch them, it will move the camera to the center of the new room and put you infront of the door you came out of. All rooms are equal sizes so you always move the same amount. Each door has a randomized variable telling it which side of the room it is on. I need a way for the player object to get that variable from that specific door. Thank you for help.
    (2D, Javascript)

    Code (JavaScript):
    1. function OnTriggerEnter2D (obj : Collider2D) {
    2.     var name = obj.gameObject.name;
    3. if(name == "door") {
    4.    
    5.    
    6.     }
    7.     }
    8.    
     
  2. roger0

    roger0

    Joined:
    Feb 3, 2012
    Posts:
    1,208
    Use GetComponent to find a script on a object, and then access the variable on the script.

    Code (CSharp):
    1. function OnTriggerEnter2D (obj : Collider2D) {
    2.     var name = obj.gameObject.name;
    3. if(name == "door") {
    4.    obj.gameObject.GetComponent(scriptName).variableName
    5.  
    6.     }
    7.     }