Search Unity

Bug My rectangle is invisible when I instantiate it

Discussion in 'UGUI & TextMesh Pro' started by penguinslapp, Jul 31, 2022.

  1. penguinslapp

    penguinslapp

    Joined:
    Mar 21, 2022
    Posts:
    6
    I made a function to make rectangles representing shields appear across the bottom of the screen and be able to resize them as more are added, but whenever I create a rectangle its invisible. I tried deleting its image component and replacing it with that of a rectangle that actually shows, but it's still invisible. Here's a screenshot of the inspector: upload_2022-7-30_18-8-44.png
    and here's the code in my function (i haven't gotten around to debugging it yet, just trying to get the rects to show):
    Code (CSharp):
    1.     void onIncreaseMax(int amountIncrease)
    2.     {
    3.         amountofshields += amountIncrease;
    4.         foreach (GameObject item in shields)
    5.         {
    6.             Destroy(item);
    7.         }
    8.         float widthpershield = shieldswidth / amountofshields;
    9.         float shieldscenter = widthpershield / 2;
    10.         List<Vector2> tospawns = new List<Vector2>();
    11.         for (int i = 0; i < amountofshields; i++)
    12.         {
    13.             Vector2 toadd = new Vector2(shieldscenter, shieldsheight);
    14.             tospawns.Add(toadd);
    15.         }
    16.         List<Vector2> sizes = new List<Vector2>();
    17.         for (int i = 0; i < amountofshields; i++)
    18.         {
    19.             Vector2 toadd = new Vector2(widthpershield - spacebetween, segmentheight);
    20.             sizes.Add(toadd);
    21.         }
    22.         for (int i = 0; i < amountofshields; i++)
    23.         {
    24.             shields.Add(Instantiate(shieldori, tospawns[i], Quaternion.identity));
    25.         }
    26.         int eye = 0;
    27.         foreach (GameObject item in shields)
    28.         {
    29.             RectTransform rct = item.GetComponent<RectTransform>();
    30.             rct.sizeDelta = sizes[eye];
    31.             eye++;
    32.         }
    33.     }
     
  2. penguinslapp

    penguinslapp

    Joined:
    Mar 21, 2022
    Posts:
    6
    Turns out I wasn't childing my rectangles to my canvas. whoops