title: Unity: Dynamically build UI elements on Canvas: ScreenSpace-Camera, previously ScreenSpace-Overlay
body: <p>My application works fine, 100% expected results with Canvas set on ScreenSpace-Overlay. I have written a function that takes an array of integers and based on its values it dynamically builds vertical bars inside a panel. I have an UI-Image as a prefab and that I instantiated multiple time, set its anchor to bottom left position and increase the x-offset from that point in increments of its width+padding. Everything works fine, check the screenshots.</p>
  <p>I have now a fancy idea of animating the vertical bars... to have Unity Particle Effects on the top of each bar and to play it. From what I searched... you cannot get Particle Effects working on Canvas-UI unless the Canvas is set as ScreenSpace-Camera. I managed to obtain success with Particle Effects and ScreenSpace-Camera canvas... everything works... but now the same code that dynamically builds the UI doesnt work anymore... the vertical bars are set on a total new position outside the UI.
  <a href="https://i.stack.imgur.com/fpJP6.png" rel="nofollow noreferrer">My Canvas</a>
  <a href="https://i.stack.imgur.com/d6wm7.png" rel="nofollow noreferrer">Working algorithm</a>
  My code:</p>
  <pre><code>private void ArrangeImagesOnCanvas(List&lt;int&gt; varArray)
  {
      Canvas canvas = FindObjectOfType&lt;Canvas&gt;();
      //float h = canvas.GetComponent&lt;RectTransform&gt;().rect.height * canvas.GetComponent&lt;RectTransform&gt;().localScale.y;
      //float w = canvas.GetComponent&lt;RectTransform&gt;().rect.width * canvas.GetComponent&lt;RectTransform&gt;().localScale.x;
  
      float h = _panelImageForSortingAlgorithm.GetComponent&lt;RectTransform&gt;().rect.height * canvas.GetComponent&lt;RectTransform&gt;().localScale.y;
      float w = _panelImageForSortingAlgorithm.GetComponent&lt;RectTransform&gt;().rect.width * canvas.GetComponent&lt;RectTransform&gt;().localScale.x;
  
      float minH = 20.0f;
      float maxH = h - 20;
  
      int minList = varArray.Min();
      int maxList = varArray.Max();
  
      _imageWidth = (w - (varArray.Count * _PADDING)) / varArray.Count;
  
      List&lt;float&gt; heightsList = new List&lt;float&gt;();
  
      float _tempFloat = 0;
      for (int i = 0; i &lt; varArray.Count; i++)
      {
          _tempFloat = ScaleIntervals(varArray[i], new Vector2(minList, maxList), new Vector2(minH, maxH));
          heightsList.Add(_tempFloat);
      }
  
      for(int i = 0; i&lt;_instancesOfImages.Count;i++)
      {
          Destroy(_instancesOfImages[i]);
      }
      _instancesOfImages.Clear();
      for (int i = 0; i &lt; varArray.Count; i++)
      {
          GameObject instantiatedImage = Instantiate(_imageToInstance, _imageToInstance.transform.position, Quaternion.identity);
          instantiatedImage.transform.SetParent(_panelImageForSortingAlgorithm.transform);
          Image img = instantiatedImage.GetComponent&lt;Image&gt;();
          img.rectTransform.anchorMin = new Vector2(0, 0);
          img.rectTransform.anchorMax = new Vector2(0, 0);
          img.rectTransform.pivot = new Vector2(0, 0);
  
          img.rectTransform.position = new Vector3(i * (_imageWidth + _PADDING), 0, 0);
          img.rectTransform.sizeDelta = new Vector2(_imageWidth, heightsList[i]);
  
          _instancesOfImages.Add(instantiatedImage);
      }
  }
  </code></pre>
  <p>For the same code the vertical bars can be seen in this image...
  <a href="https://i.stack.imgur.com/1EadR.png" rel="nofollow noreferrer">Broken bars 1</a>
  if I put</p>
  <pre><code>float h = _panelImageForSortingAlgorithm.GetComponent&lt;RectTransform&gt;().rect.height;
  float w = _panelImageForSortingAlgorithm.GetComponent&lt;RectTransform&gt;().rect.width;
  </code></pre>
  <p>then the vertical bars are like in this image.
  <a href="https://i.stack.imgur.com/AZD8f.png" rel="nofollow noreferrer">Broken bars 2</a></p>
  <p>How can I position correctly the vertical bars as in ScreenSpace-Overlay but this time to be on ScreenSpace-Camera ? Please help or give me some advices or tips...</p>