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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Code not working in build

Discussion in 'Scripting' started by coolcow9, Apr 12, 2020.

  1. coolcow9

    coolcow9

    Joined:
    Aug 21, 2017
    Posts:
    2
    I've made a script to handle buttons for my game UI. The posted section of the script resizes the background of the button based on text length. It works perfectly in the editor testing, but when I make a build, the code stops working.

    Code (CSharp):
    1. void Start()
    2.     {
    3.             text = GetComponent<Text>();
    4.             rect = GetComponent<RectTransform>();
    5.  
    6.             TextAnchor currentAncor = text.alignment;
    7.             text.alignment = TextAnchor.UpperCenter;
    8.  
    9.             float width = CalculateLengthOfMessage(text.text) - 520f;
    10.             float offsetX = width / 2 - 2f;
    11.  
    12.             text.alignment = currentAncor;
    13.  
    14.             float centeredOffset = text.alignment == TextAnchor.UpperLeft ? width / 8 : 0;
    15.  
    16.             transform.GetChild(0).localScale = new Vector3(width * 2, transform.GetChild(0).localScale.y, 1);
    17.             transform.GetChild(0).localPosition = new Vector3(centeredOffset, 0, 0);
    18.             transform.GetChild(1).localPosition = new Vector3(centeredOffset - offsetX, 1.9f, 0);
    19.             transform.GetChild(2).localPosition = new Vector3(centeredOffset + offsetX, 1.9f, 0);
    20.  
    21.             transform.GetComponent<BoxCollider>().size = new Vector3(width + 520f, 360f, 1f);
    22.     }
    23.  
    24. int CalculateLengthOfMessage(string message)
    25.     {
    26.         int totalLength = 0;
    27.  
    28.         Font myFont = text.font;  //chatText is my Text component
    29.         CharacterInfo characterInfo = new CharacterInfo();
    30.  
    31.         char[] arr = message.ToCharArray();
    32.  
    33.         foreach (char c in arr)
    34.         {
    35.             myFont.GetCharacterInfo(c, out characterInfo, text.fontSize);
    36.  
    37.             totalLength += characterInfo.advance;
    38.         }
    39.  
    40.         return totalLength;
    41.     }
    42.  
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    Where do you set the text in the text object?
     
  3. coolcow9

    coolcow9

    Joined:
    Aug 21, 2017
    Posts:
    2
    I set the text in the editor.
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    The only thing I can think of is for some reason, the text isn't there when this code runs. Doesn't seem correct to me, but I'm assuming this code is running which just means something else is off.

    I suggest using something like https://assetstore.unity.com/packages/tools/integration/log-viewer-12047 and adding some debug messages to print out some values in your code so you can see what they are in the build and make sure you are getting what you should be getting. (you can compare them to the editor).

    This will also assure you that code is running as it should be.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    The problem is likely your use of the magic number 520 in line 9.

    If that is intended to map to "half the width of the screen," then use Screen.width / 2 instead.

    If it is intended to map to something else, use the Canvas or some other interim RectTransform object to obtain that dimension at runtime. It's unlikely the target will run at a constant dimension or that this dimension will match your editor window.

    I see you use other magic numbers such as 520 and 360 and even 1.9f... those might be causing problems too.
     
    Brathnann likes this.