Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

While statement not calling when calling a function from another script

Discussion in 'Scripting' started by jessee03, Dec 14, 2014.

  1. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    I'm having a really weird issue right now. For some reason when I try calling a function from another script it will call the function but won't trigger my while statement. Which if I call the function from the main script the while statement always gets called and the function plays through as expected. I noticed when calling the function from another script it does change my first boolean to true, which it should but then after that it just doesn't call my while statement. Not sure what could be wrong since the function works perfectly fine when called in the main script where the functions located.

    Code (csharp):
    1.  
    2. //Script 1
    3.  
    4. function slotMachineTrigger () {
    5.  
    6.     slotWindow = true;
    7.  
    8.     while(slotWindow) {
    9.         yield WaitForSeconds(0.1);
    10.         slotIconMain = slotIconRandom[Random.Range(0,2)];
    11.         slotTextMain = slotTextRandom[Random.Range(0,5)];
    12.    
    13.     }
    14.  
    15. //Script 2
    16. //
    17.     var MainScript : Main_Gameplay;
    18.     MainScript = gameObject.Find("Main Camera").GetComponent(Main_Gameplay);
    19.  
    20. MainScript.slotMachineTrigger();
    21.  
    The weird thing is if I call a different function that triggers my other function it works and does call the while loop and the rest of the code.

    Code (csharp):
    1.  
    2. //script 1
    3. function testFunction () {
    4.  
    5. slotMachineTrigger();
    6.  
    7. }
    8.  
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    You probably need it to be public. I think it's accessing the while loop, but it isn't changing those variables. You could put a debug inside the while loop to test it. I don't use js, so maybe I'm wrong about that.
     
  3. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    When I call the function from another script it does call slotWindow =true; Yet it doesn't trigger the while loop.So the function is being called
     
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    If it changed slotWindow, it pretty much had to go into the yield, because that was the condition needed. Did you try a Debug.Log("in while loop");, inside the while loop? Are you using a debugger and it just skips it or what?
     
    Last edited: Dec 15, 2014
  5. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    What I was saying in my original post is if I call that function from inside of my first script then the function calls everything. If the function is called from a different script then it just activates the first piece of code slotWindow =true; The while loop should be activating the same way no matter where the function is being called from.
     
  6. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    In c#, it has to be a public function, but they give errors and whatnot. I know I've gotten them before, also when trying to change variables that aren't public, but I think js variables are public by default.
    This is from the script reference:
    function Update () {
    // To access public variables and functions // in another script attached to the same game object. // (ScriptName is the name of the javascript file) var other : ScriptName = gameObject.GetComponent(ScriptName);
    // Call the function DoSomething on the script other.DoSomething ();
    // set another variable in the other script instance other.someVariable = 5;
    }
     
    Last edited: Dec 15, 2014