-1

In my project I have a canvas and now I want to spawn a TMP_InputField on it using script. For other things like Image, TMP_Text, etc. I simply generated a new GameObject and added the necessary components to it:

GameObject object = new GameObject("Object", typeof(Image));
object.transform.SetParent(this.transform);

But for TMP_InputField this doesn't work as expected. I can add the components to it and have an GameObject in my hierarchy but I don't know if I'm missing any components and how to link the ones I have up. Please help me. I can provide my code if requested although a clean and new solution would be more helpful.

wejoey
  • 216
  • 1
  • 3
  • 14
ZoKi Mo
  • 51
  • 1
  • 5
  • 3
    Create an instance of an input field using the editor. You will see there is much more to an input field than just a single script on a game object. – hijinxbassist May 17 '23 at 18:57

1 Answers1

0

Easy solution, made a prefab with the component that you want to add, then instantiate the prefab on the canvas.

Example:

    public TMP_InputField inputFieldPrefab;
    private TMP_InputField instantiatedInputField;
    public Transform parent

    private void InstantiateInputFieldPrefab()
    {

        instantiatedInputField = Instantiate(inputFieldPrefab, parent);
        // do the setup on the instance 
    }
Mario
  • 601
  • 3
  • 7