Instantiating Button/Text Prefab Does Not Work
Hello, I am quite new to Unity and so this may seem quite simple, but I've been stuck on this problem for a while. I want the user of my app to be able to create text and buttons during runtime. I am trying to instantiate the text and button prefabs, but it will not work/show. I've tried instantiating simple things like a basic sphere and it appeared perfectly on the world space. I've read in several threads that I need to set the parent of the prefab to the canvas, which I am trying, but that does not seem to work. Here is a snippet of my code. Ignore the _quad, that is necessary for the prefab positioning. If I accidentally stripped too much code, and you feel there is more details needed, please let me know.
public Text TextPrefab;
public Canvas ControlCanvas;
public void InstantiateTextPrefab()
{
Vector3 SurfacePoint = eventData.pointerCurrentRaycast.worldPosition;
Canvas newCanvas = Instantiate<Canvas>(ControlCanvas);
Text newText = Instantiate(TextPrefab, _quad.OffsetForDrawing(SurfacePoint), Quaternion.identity) as Text;
newText.transform.SetParent(ControlCanvas.transform, false);
}
I think I've found the reason for the bug: I'm getting an ArgumentException: The Object you want to instantiate is null. What I can't figure out is why I'm getting that exception. I've dragged the Text Prefab and Canvas Prefab into their respective positions in the inspector view of the script, and the Prefabs definitely do exist.
Your answer
Follow this Question
Related Questions
Instantiated button prefab causes Delegation error on click and does not add OnClick Listener 0 Answers
Issue with instantiated text clone 0 Answers
2018.2 text mesh pro. plugin Errors. 7 Answers
Text adventure game, how to change text in a specific way with C# 1 Answer
send the text of a button as variable 0 Answers