Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

(Solved) removing an instantiated InputField, not working - effectively disables all input

Discussion in 'Scripting' started by SomeVVhIteGuy, Nov 27, 2018.

  1. SomeVVhIteGuy

    SomeVVhIteGuy

    Joined:
    Mar 31, 2018
    Posts:
    162
    hello,

    Setup;
    Gameobject ActiveCharacterInfo (ACI), which holds a "stat" storing script name CharacterInfo (CI). One of those stats is the character name

    A UI Button calls a method in (CI) which instantiates a prefab InputField (NameChangeBox). the only extra script on that prefab is as follows:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class NameChangeFieldScript : MonoBehaviour
    7. {
    8.     GameObject activeCharacter;
    9.     InputField nameInputField;
    10.  
    11.     private void OnEnable()
    12.     {
    13.         nameInputField = gameObject.GetComponent<InputField>();
    14.         activeCharacter = GameObject.FindGameObjectWithTag("Player");
    15.         nameInputField.onEndEdit.AddListener(afterReturn);
    16.     }
    17.     private void afterReturn(string iName)
    18.     {
    19.         activeCharacter.GetComponent<CharacterInfo>().updateName(iName)
    20.         Destroy(gameObject.GetComponent<InputField>());
    21.         //Debug.Log(iName);
    22.     }
    23.  
    24. }
    ctiveCharacter.GetComponent<CharacterInfo>().updateName(iName) WORKS FINE, it deletes the old save and replaces it with a new one under a new character name.

    Destroy(gameObject.GetComponent<InputField>()); does not work. it also does not work when used as Destroy(nameInputField);

    Both Destroy() methods ive tried seems to disable the input field component, as I cannot click on it anymore or input anything. It still on screen though, and still in the Hierarchy.

    If I click the button again, it instantiates a new iField. I don't know what to try form here. I did attempt to redefine everything as a plain Gameobject but that ran me into more issues. any advice would be awesome
     
  2. SomeVVhIteGuy

    SomeVVhIteGuy

    Joined:
    Mar 31, 2018
    Posts:
    162
    Christ. Fastest I've ever solved something after asking.

    Destroy(nameInputField.gameobject);

    thanks for looking anyway!