Reputation: 29
I am trying to instantiate many world canvases and it gives me this error:
UnityEngine.UI.Text.Text()' is inaccessible due to its protection level, UnityEngine.UI.Image.Image()' is inaccessible due to its protection level
I have created an prefab which contains a canvas set to world space, and I can't understand the right approach to instantiate them. here is a copy of the script:
public void PositionWorldCanvases ()
{
for (int i = 0; i < vertices.Length; i++) {
Canvas can = new Canvas();
Text text = new Text();
Image image = new Image();
Instantiate (canvasManager, vertices[i], Quaternion.identity);
can = canvasManager.GetComponentInChildren<Canvas> ();
image = can.GetComponent<Image> ();
text = image.GetComponent<Text> ();
text.text = "Hello World";
}
}
Upvotes: 0
Views: 55
Reputation: 8163
Its not a problem with instantiating, its a problem with construction.
Text
and Image
have private
constructors, so they cannot be created out of scope using the new
keyword, you have to attach them manually to an empty GameObject
with a RectTransform
and CanvasRenderer
component. then you instantiate that object.
Upvotes: 1