title: How to remove spacing between buttons in Unity3D?
body: <p>I'm creating buttons programmatically in a game I'm designing in Unity. I instantiate the button prefab and parent them to the Panel object on the canvas. I've set the Panel to have a Horizontal Layout Group with the following properties:</p>
  
  <p><a href="https://i.stack.imgur.com/SM78j.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SM78j.png" alt="enter image description here"></a></p>
  
  <p>I've added the following components to the button prefab to achieve the look I require:</p>
  
  <p><a href="https://i.stack.imgur.com/Wwbbg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Wwbbg.png" alt="enter image description here"></a></p>
  
  <p>After doing all this here's the result I end up with:</p>
  
  <p><a href="https://i.stack.imgur.com/j2t53.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/j2t53.png" alt="enter image description here"></a></p>
  
  <p>Therefore, my question is, how can I remove the spacing between each of the buttons? The buttons are supposed to be right next to each other (but not overlapping) to look like a sentence. The reason why I've got separate buttons is so that the user needs to click the correct 2 letter characters in the sentence (in the above example is the letters <code>ar</code>). I've set the spacing on the properties to <code>0</code> and checked/unchecked the various options but no luck!</p>
  
  <p>I've unchecked the width option for Child Force Expand on both the Panel and Button objects but the sentence looks all crammed.... how could I ensure the buttons sit neatly one after the other (i.e not overlapping)? </p>
  
  <p><a href="https://i.stack.imgur.com/hwCsV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hwCsV.png" alt="enter image description here"></a></p>
  
  <p>Code relating to comment below:</p>
  
  <pre><code> for (int i = 0; i &lt; letterArray.Length; i++)
          {
              button = (GameObject)Instantiate(buttonPrefab);
              button.transform.SetParent(buttonPanel.transform, false);
              button.GetComponentInChildren&lt;Text&gt;().text = letterArray[i];
  
              FitButtonWidthToText();
          }
      }
  
      public void FitButtonWidthToText()
      {
          int widthOfTheText = 0;
          CharacterInfo characterInfo;
          foreach (char c in button.GetComponentInChildren&lt;Text&gt;().text)
          {
              button.GetComponentInChildren&lt;Text&gt;().font.GetCharacterInfo(c, out characterInfo, button.GetComponentInChildren&lt;Text&gt;().fontSize);
              widthOfTheText += characterInfo.advance;
          }
          buttonTransform.sizeDelta = new Vector2(widthOfTheText, buttonTransform.sizeDelta.y);
      }
  </code></pre>