Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Script help. How do i call this function.

Discussion in 'Scripting' started by Survivorman198, Jan 1, 2015.

  1. Survivorman198

    Survivorman198

    Joined:
    Nov 21, 2014
    Posts:
    25
    I Got this script for a gui. I have script A

    Code (JavaScript):
    1. var Active : boolean = false;
    2.     function Update ()
    3.     {
    4.         var Dissable_button : Dissable_button = GetComponent(Dissable_button);
    5.         if (Input.GetKeyDown ("escape")){
    6.         if (Active == false){      
    7.         animation.Play ("A_in");
    8.         Active = true;
    9.         Screen.lockCursor = false;
    10.            Dissable_button.Toggle(); //Acsesses script B's Function
    11.     }
    12.     }
    13.     }
    14.     function Close ()
    15.     {
    16.         animation.Play ("A_out");
    17.         Active = false;
    18.          Screen.lockCursor = true;
    19.          Dissable_button.Toggle2(); //Acsesses script B's Function
    20.     }
    Then there is script B

    Code (JavaScript):
    1.    
    2. private var button : UnityEngine.UI.Button;
    3.  
    4.  
    5.     function Start ()
    6.     {
    7.         buttont = GetComponent(UnityEngine.UI.Button);
    8.     }
    9.    
    10.     function Toggle () // The function script A is calling
    11.     {
    12.         button.enabled = !button.enabled;
    13.          print("Toggled!");
    14.     }
    15.     function Toggle2 () // The function script A is calling
    16.     {
    17.         button.enabled = !button.enabled;
    18.          print("Toggled!");
    19.     }
    The to scripts are in two different objects. Script a is calling function's in script B.
    Why is it not working.
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,083
    When you use 'GetComponent(Dissable_button)' that would only work if both components were in the same object. If script B is in a different object you would need to do something like this in Script A:

    var Dissable_button : Dissable_button = ObjectB.GetComponent(Dissable_button);

    'ObjectB' would be a GameObject in Script A that you would set to point to the object that contains Script B.
     
  3. Survivorman198

    Survivorman198

    Joined:
    Nov 21, 2014
    Posts:
    25
    The object script B is in is a child of a child.
    How do I fix that.
     
  4. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    transform.GetChild(child_number).GetComponent
     
  5. Survivorman198

    Survivorman198

    Joined:
    Nov 21, 2014
    Posts:
    25
    Were do I put this?
     
  6. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    In @tonemcbride 's example, replace ObjectB.GetComponent with transform.GetChild(0).GetComponent<ScriptName>();

    Looks like your script name is Dissable_button so GetComponent<Dissable_button>
     
  7. Survivorman198

    Survivorman198

    Joined:
    Nov 21, 2014
    Posts:
    25
    Ok here is want the script A looks like
    Code (JavaScript):
    1. var Active : boolean = false;
    2.     function Update ()
    3.     {
    4.         var Dissable_button : Dissable_button = transform.GetChild(2).GetComponent(Dissable_button)();
    5.  
    6.         if (Input.GetKeyDown ("escape")){
    7.         if (Active == false){      
    8.         animation.Play ("A_in");
    9.         Active = true;
    10.         Screen.lockCursor = false;
    11.            Dissable_button.Toggle(); //Acsesses script B's Function
    12.     }
    13.     }
    14.     }
    15.     function Close ()
    16.     {
    17.         animation.Play ("A_out");
    18.         Active = false;
    19.          Screen.lockCursor = true;
    20.          Dissable_button.Toggle2(); //Acsesses script B's Function
    21.     }
    Now I am getting the error | Assets/Pause_menu.js(4,91): BCE0077: It is not possible to invoke an expression of type 'Dissable_button'
     
  8. Survivorman198

    Survivorman198

    Joined:
    Nov 21, 2014
    Posts:
    25
    By the way, thanks for the help.
     
  9. Tomnnn

    Tomnnn

    Joined:
    May 23, 2013
    Posts:
    4,148
    In C# the code wold be GetComponent<script>(); but you're in js so I think the code is GetComponent(script);

    Looks like you can just remove the extra () from line 4 before the semicolon.