Change the Height and Width of a RectTransform Component

Hey
I am having a problem where I am creating a new GameObject and can’t seem to change the sizeDelta of it’s RectTransform. I just get the error "
Assets\Scripts\Ship Builders\BoatShopList.cs(31,19): error CS1061: ‘GameObject’ does not contain a definition for ‘RectTransform’ and no accessible extension method ‘RectTransform’ accepting a first argument of type ‘GameObject’ could be found (are you missing a using directive or an assembly reference?)"

Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class BoatShopList : MonoBehaviour
{
    public int Height;
    public int tb;
    public TextAsset file;
    public int i = 0;
    public string PanelName;
    GameObject gameObjectShipBuilders;
    // Start is called before the first frame update
    void Start()
    {
        Load(file);
        tb = int.Parse(Find_number_entries("TRUE").name);
        Height = tb * 105;
        RectTransform rt = GetComponent<RectTransform>();
        rt.sizeDelta = new Vector2(4, Height);
        gameObjectShipBuilders = GameObject.Find("Content");
        while (i < tb)
        {
            PanelName = "Panel" + i;
            GameObject panel = new GameObject(PanelName);
            panel.AddComponent<CanvasRenderer>();
            panel.AddComponent<Image>();
            panel.transform.parent = gameObjectShipBuilders.transform;
            panel.RectTransform.sizeDelta = new Vector2(4, 95);
            i++;
        }
    }

Thanks in advance!

panel.RectTransform.sizeDelta = new Vector2(4, 95);

Replace this with GetComponent and use it like you do on lines 21 & 22.

Hey
I tried out what you said an I now get a different error: "
Assets\Scripts\Ship Builders\BoatShopList.cs(31,19): error CS0119: ‘GameObject.GetComponent()’ is a method, which is not valid in the given context"
I changed the code in accordance to what you said is this what I was meant to do?

        while (i < tb)
        {
            PanelName = "Panel" + i;
            GameObject panel = new GameObject(PanelName);
            panel.AddComponent<CanvasRenderer>();
            panel.AddComponent<Image>();
            panel.transform.parent = gameObjectShipBuilders.transform;
            panel.GetComponent<RectTransform>.sizeDelta = new Vector2(4, 95);
            i++;
        }

Thanks for the reply.

You forgot ().

Thank you very much, it works absolutely perfectly now :slight_smile: