Search Unity

Switching on and off on collision

Discussion in 'Scripting' started by willgoldstone, Dec 15, 2006.

  1. willgoldstone

    willgoldstone

    Unity Technologies

    Joined:
    Oct 2, 2006
    Posts:
    794
    Hi guys,

    struggling with what should be basic, we're switching a GUI text on with the following script when u walk into an object with the FPS controller, that has a tag called "head1" -

    Code (csharp):
    1.  
    2. function OnControllerColliderHit (hit : ControllerColliderHit){
    3.    
    4.     if (hit.collider.gameObject.tag == "head1") {
    5.        
    6.         switcher.switcher();
    7.  
    8.     }
    9.    
    10. }
    11.  
    This references a static function called "switcher" in a script also called "switcher", below -

    Code (csharp):
    1.  
    2.  
    3. //function to switch GUI Texture on / off
    4. static function EnableTaggedGUIText(tag : String, enable : boolean) {
    5.  
    6.     //check for an object with a certain tag
    7.    var go = GameObject.FindWithTag(tag);
    8.    if(go) {
    9.    //   if found, address its GUITexture component
    10.       var gt = go.GetComponent(GUIText);
    11.       if(gt) {
    12.        //..and enable it
    13.         gt.enabled=enable;
    14.       }
    15.      
    16.      // if not found state that there is no GUITexture on the gameObject
    17.       else {
    18.           Debug.Log("Error: The GameObject does not have a GUIText component to enable", go);
    19.       }
    20.    }
    21.    //if no tagged game object matching found, return following statement
    22.    else {
    23.       Debug.Log("Error: Could not find a GameObject tagged "+tag);
    24.    }
    25. }
    26.  
    27.  
    28.  
    29. function Start(){
    30.  
    31.         EnableTaggedGUIText("head", false);
    32.  
    33. }
    34.  
    35.  
    36. static function switcher(){
    37.  
    38.     EnableTaggedGUIText("head", true);
    39.    
    40. }
    41.  
    42. static function off(){
    43.  
    44.     EnableTaggedGUIText("head", false);
    45.    
    46. }
    47.  
    This script ^^ is attached to the GUItext itself.

    That all works fine, but as you can see in the switcher script, we also have a function called "off" to switch off our GUIText, and we need to trigger this when we walked away from the object that has the "head1" tag.

    Surely this is a straightforward thing to do? Cant seem to use CollisionExit because it doesnt work with character collider. Can anyone suggest a straightforward way of doing this? Tearing my hair out on this one...


    Thanks


    Will[/code]