Can't see elements added to panel

Hello,

I have created an info window script to show informations panel above selected element in VR scene using HTC Vive.

I have made a GameObject in the hierarchy and associated a C# script.

In the script I created a canvas named “InfoWindow” and added a panel. I setted a black background using an image to the panel so I can see it on screen.

The problem is when I try to add other element to the panel. Nothing appear on it.

Here is my code.

void Start () {
this.infoWindow = new GameObject(“IDCInfoWindow”);
this.infoWindow.AddComponent().sizeDelta = new Vector2(900, 900);
this.infoWindow.GetComponent().localScale = new Vector3(0.005f, 0.005f, 0);
Canvas c = this.infoWindow.AddComponent();
c.renderMode = RenderMode.WorldSpace;
this.infoWindow.AddComponent();
this.infoWindow.AddComponent();
this.panel = new GameObject(“Panel”);
this.panel.AddComponent();
this.panel.transform.SetParent(this.infoWindow.transform, false);
// Ajout background
Image i = this.panel.AddComponent();
i.rectTransform.sizeDelta = new Vector2(900, 900);
i.color = Color.black;

// Ajout text titre
GameObject titleTextGO = new GameObject(“titleTextGO”);
titleTextGO.transform.SetParent(this.panel.transform);
titleTextGO.AddComponent();
this.titleText = titleTextGO.AddComponent();
this.titleText.text = “Liste des documents technique liés à la vanne VLV1”;
this.titleText.fontSize = 50;
this.titleText.color = Color.white;
//this.titleText.rectTransform.position = new Vector3(0, 0, 0);
this.titleText.rectTransform.sizeDelta = new Vector2(400, 400);
this.titleText.GetComponent().anchoredPosition = new Vector3(0, 0, 0);
this.titleText.GetComponent().localScale = new Vector3(1, 1, 0);

// Ajout Bouton Atex
this.btAtex = new GameObject(“BoutonAtex”);
btAtex.transform.SetParent(this.panel.transform);
Button bt1 = this.btAtex.AddComponent();
ColorBlock cb = bt1.colors;
cb.normalColor = Color.white;
bt1.colors = cb;

//this.infoWindow.SetActive(false);
}

I’ve done all the creation inside the start method.

I hope you have an idea.

Thank you,

Y.

Couple of things here…

  1. Please read this page about how to post code on the forums nicely: Using code tags properly
  2. rather than try to figure out what’s the matter right now with your code, what do you say to trying by creating this object you desire, making it a prefab, and then referencing it to be instantiated instead of all that code?
    Don’t get me wrong, I love code… I’m just saying maybe give that a shot. :slight_smile:

One note, though, I thought it wasn’t possible to add a rect transform to an object. One would just be assigned automatically if you add an appropriate object to a canvas (such as a panel).