Search Unity

Editor Stops Responding After Certain Script Runs

Discussion in 'Scripting' started by YBtheS, Jul 2, 2018.

  1. YBtheS

    YBtheS

    Joined:
    Feb 22, 2016
    Posts:
    239
    Hello,
    I'm creating a multiplayer game using Photon Unity Networking. When a player joins a room (that has already been created), they instantiate (locally, not over the network) a "Manager" game object which is responsible for setting up all other local game objects and scripts. When I join a room from a standalone build, I get an error on another script called ColorSelector.cs. Since I downloaded a console program from the Unity Asset Store, I can see anything the logs to the console including the error. Unfortunately it is not as detailed as the Unity editor console so I can't see what line the error occurred on however I can see what function of the script is in. The error occurs in the UserInputUpdate method. Here is a snippet of the script:

    Code (CSharp):
    1. void Update () {
    2.         if (Input.GetMouseButton (0)) {
    3.             UserInputUpdate ();
    4.         }
    5.     }
    6.  
    7.     void UserInputUpdate(){
    8.         Vector3 cursorPos = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, (transform.position.z - refCamera.transform.position.z));
    9.         Ray cursorRay = refCamera.ScreenPointToRay (cursorPos);
    10.         RaycastHit hit = new RaycastHit ();
    11.         if(Physics.Raycast(cursorRay,out hit)){
    12.             Vector3 localPosition=transform.InverseTransformPoint(hit.point);
    13.             float dist=Vector2.Distance(Vector2.zero,localPosition);
    14.  
    15.            
    16.             placePixel.ignore = true;                //MODIFIED: Added code to prevent placing a pixel when selecting a color
    17.  
    18.             if(dist>0.22)
    19.                 SelectOuterColor(localPosition);
    20.             else
    21.                 SelectInnerColor(localPosition);
    22.  
    23.         }
    24.     }
    25. void SelectInnerColor(Vector2 delta){
    26.         float v=0.0f, w=0.0f, u=0.0f;
    27.         Barycentric (delta,ref v,ref w,ref u);
    28.         if (v >= 0.15f && w >= -0.15f && u >= -0.15f) {
    29.             Vector3 colorVector = new Vector3 (selectedColor.r, selectedColor.g, selectedColor.b);
    30.             Vector3 finalColorVector = v * colorVector + u * new Vector3 (0.0f, 0.0f, 0.0f) + w * new Vector3 (1.0f, 1.0f, 1.0f);
    31.             finalColor = new Color (finalColorVector.x, finalColorVector.y, finalColorVector.z);
    32.  
    33.             finalColorSprite.color=finalColor;
    34.             innerCursor.transform.localPosition =delta;
    35.             innerDelta = delta;
    36.  
    37.        
    38.  
    39.         }
    40.  
    41.     }
    42. void SelectOuterColor(Vector2 delta){
    43.         float angle= Mathf.Atan2(delta.x, delta.y);
    44.         float angleGrad=angle*57.2957795f;
    45.         if(angleGrad<0.0f)
    46.             angleGrad=360+angleGrad;
    47.         selectorAngle=angleGrad/360;
    48.         selectedColor=HSVToRGB(selectorAngle,1.0f,1.0f);
    49.         selectorImage.GetComponent<Renderer>().material.SetColor("_Color",selectedColor);
    50.         outerCursor.transform.localPosition = ClampPosToCircle (delta);/// delta*0.75f;
    51.         SelectInnerColor (innerDelta);
    52.  
    53.     }
    The odd part that makes this hard to debug is that if I attempt to join a room using the Unity Editor and not the standalone build, before the error even prints in the console, Unity stops responding.

    What is the error that causes this behavior?
    Thanks in advance!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    You're going to have to slice and dice this a bit more, start removing pieces of functionality until it doesn't crash, narrow down until you get a smoking gun. The good news is you only have about 25 lines of actual code to turn off. :)

    Start at the inner part first: remove the if statement that either calls SelectOuterColor/InnerColor. If it still crashes, put that back and move up the chain. Remove the entire result of the if( Physics.Raycast). If that still crashes, then remove the Physics raycast, etc.
     
  3. YBtheS

    YBtheS

    Joined:
    Feb 22, 2016
    Posts:
    239
    I was afraid of having to do that because restarting Unity takes some time. Will do soon and update you. Thanks for the help!
     
  4. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    You're performing too many operations per user input.
    This is probably not the case, but I suggest you to either change it to GetMouseButtonDown, or move the whole thing to the FixedUpdate, and execute it only when input is received, and flag is set.
     
  5. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    On line 51 you are passing innerDelta into SelectInnerColor(innerDelta); before it's set. should that not be SelectInnerColor(delta);

    I'd set a breakpoint and step through it line by line and see where it fails.
     
  6. YBtheS

    YBtheS

    Joined:
    Feb 22, 2016
    Posts:
    239
    GetMouseButton is necessary in this case unfortunately. Why would a FixedUpdate better? Isn't that unreliable for any input?

    Note that I did not post the full code. Only the things that I thought pertained to the problem (so the method that the error occurs in and the two methods that it calls). I will check to see if innerDelta is set somewhere else.
     
  7. YBtheS

    YBtheS

    Joined:
    Feb 22, 2016
    Posts:
    239
    Huh, so now I'm quite confused. I started placing return statements everywhere in the UserInputUpdate method and Unity kept not responding. Eventually, I put it in the very first line of the method and still got the same result so it would seem that the method isn't the problem. Perhaps the Editor is stuck on something completely unrelated to the error in the standalone build.

    I noticed a suspicious line. It says,
    while(!placePixel) {/*Wait for placePixel to exist or something*/}
    Can an infinite loop cause Unity to stop responding? That'll cause a stack overflow error at the very least right?

    As for innerDelta, the only other mentions of it are here:

    Code (CSharp):
    1. Vector2 innerDelta=Vector2.zero;
    2.     static ColorSelector myslf;
    3.  
    4.     void Awake () {
    5.         myslf = this;
    6.     }
    7.     void Start () {
    8.  
    9.         StartCoroutine(waitForColorSelector());        //MODIFIED: Added the call to the waitForColorSelector IEnumerator
    10.  
    11.         if (refCamera == null)
    12.             refCamera = Camera.main;
    13.         selectedColor = Color.red;
    14.         SelectInnerColor (Vector2.zero);
    15.         finalColorSprite.color=finalColor;
    16.  
    17.     }
    The thing is, the script works perfectly fine when I create a room and not when I join one and SelectInnerColor(InnerDelta) gets called in both scenarios so I don't think that this is the issue although it is very strange.
     
  8. Antony-Blackett

    Antony-Blackett

    Joined:
    Feb 15, 2011
    Posts:
    1,778
    infinite loops are bad.

    If you have an infinite loop it will hang unity.

    If you have a while loop in a coroutine that is there to wait for something else to complete then you must yield! inside the while loop.
     
    xVergilx and Kurt-Dekker like this.
  9. YBtheS

    YBtheS

    Joined:
    Feb 22, 2016
    Posts:
    239
    Ah okay. I will set up a coroutine and see what happens.
     
  10. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    You can receive button state in update, but perform operations in fixed update, IF that state is true.
    In this case you'll perform your operation once in a fixed amount of time, instead of each frame.

    It's up to you though.

    As mentioned above - infinite loops hang editor execution.
    Make sure you don't have somewhere a while loop, or any other loop.
    E.g. in coroutine without yield return statement.
     
  11. YBtheS

    YBtheS

    Joined:
    Feb 22, 2016
    Posts:
    239
    Ah okay. That makes sense.

    Will fix soon.
     
  12. YBtheS

    YBtheS

    Joined:
    Feb 22, 2016
    Posts:
    239
    I replaced the infinite while loop with an IEnumerator and it works now. Thanks so much!
     
    Kurt-Dekker likes this.