Search Unity

Method stops, no errors (2018.3.0f2)

Discussion in 'Scripting' started by Vedrit, Jan 14, 2019.

  1. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    Hey all, I have this mind-boggler of an issue. I have a method that I use for when a new chat message is received, but at certain points it simply...stops. No further code in the method executes, and no errors are logged.
    Code (CSharp):
    1. public void NewMessage(string charName, string newMessage)
    2.     {
    3.         Debug.Log("Received new chat message");
    4.         if (entryCount >= 100)
    5.         {
    6.             Destroy(chatDisplay.transform.GetChild(0));
    7.             entryCount--;
    8.         }
    9.         Debug.Log("5");
    10.         string replacedMessage = newMessage;
    11.         GameObject chatObject = Instantiate(entryPrefab, chatDisplay.transform); //This is the first break. The following Debug will not log unless this line is commented out.
    12.         Debug.Log("7");
    13.         if (charName == "System") //Second break. Content does not seem to matter. Tried swapping for a switch-case, same issue.
    14.         {
    15.             entryPrefab.GetComponent<Text>().color = textColors[0];
    16.         }
    17.         else
    18.         {
    19.             entryPrefab.GetComponent<Text>().color = textColors[1];
    20.         }
    21.         Debug.Log("Filter is " + filter); //Will not log if the if-statement is not commented out.
    22.         if (filter) //Third break. Same as the previous if, where the content does not seem to matter.
    23.         {
    24.             Debug.LogError("Filtering entry for user " + charName + " under parent of " + chatDisplay);
    25.             string profanity;
    26.             for (int x = 0; x < profanities.Length; x++)
    27.             {
    28.                 profanity = profanities[x];
    29.                 Regex word = new Regex("(\\b.*)(?i)(" + profanity + ")(.*\\b)");
    30.                 string temp = word.Replace(newMessage, "$1*****$3");
    31.                 replacedMessage = temp;
    32.             }
    33.         }
    34.         if (charName != lastChar) //Third verse, same as the first.
    35.         {
    36.             entryPrefab.GetComponent<Text>().text = charName + ": " + replacedMessage;
    37.             lastChar = charName;
    38.         }
    39.         else
    40.         {
    41.             entryPrefab.GetComponent<Text>().text = "       " + replacedMessage;
    42.         }
    43.         entryCount++;
    44.         Debug.Log("End of NewMessage method");
    45.         StartCoroutine(setBarTo0());
    46.     }
    This method has worked previously, though I don't recall what version it last worked in. I've not touched this part of my project in some time.
    filter is just a plain bool, set to True in editor before runtime. If stuff before it is commented out, it will return True, as expected.
     
    Last edited: Jan 14, 2019
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    I'm confused as to why your code is modifying 'entryPrefab' - you instantiate a chatObject and then modify the prefab template but that won't have any affect on your newly created item. Should you not be changing the colour and text on the 'chatObject' object?
     
  3. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    Oops, forgot to undo that test. I had changed things so 'entryPrefab' was referencing an object already in-game, rather than a prefab.
     
  4. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    Just did some fiddling and it looks like it was a threading issue caused by an asset I'm using.
     
  5. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    Ah, ok - no problem. Where does it actually stop running - do all your debug.logs happen? You could try putting a try/catch around the whole code although you would normally see any exceptions in the console window.

    Edit: Just saw your new reply, glad it's working now.