Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Unable to Modify Button after Instantiating and reaccessing it

Discussion in 'UGUI & TextMesh Pro' started by baconrad, Apr 13, 2024.

  1. baconrad

    baconrad

    Joined:
    Nov 27, 2023
    Posts:
    11
    I instantiate some objects, DisplayWord, which are essentially a button saved as a prefab. I'm able to access the button and it's text and modify it. What I want to do is allow the player to make a new word based on the orignal word in the text. So I want to update the text in the button. I am able to set the text of the button after I instantiate it. However, when I reaccess my array of DisplayWords I use essentially the same code as when I instantiated it to update the button text. However, when I try to get their script components they are null. I've written a little test to show this. I hope someone can tell me what I am doing wrong.

    Code (CSharp):
    1.  
    2. public void Start() {
    3.     //SwitchDisplays(false);
    4.     letterRack.InitializeTiles();
    5.     CreateDisplayWord("MAN");
    6.     CreateDisplayWord("CAT");
    7.     UpdateDisplayWord("CAT", "CART");
    8.     print("DisplayWord.Start \n");
    9. }
    10.  
    11. //  private DisplayWord newDisplayWord;
    12. public void CreateDisplayWord(string newWord) {
    13.     var newDisplayWord = Instantiate(displayWordPrefab, new Vector3(0, 0, 0), Quaternion.identity);
    14.     newDisplayWord.transform.SetParent(transform, false);
    15.     var displayButton = newDisplayWord.GetComponentInChildren<DisplayButton>();
    16.     displayButton.SetWord(newWord);
    17.  
    18.     //newDisplayWord.GetComponentInChildren<Button>().onClick.AddListener(() => OnButtonClick(newWord));
    19.     print("DisplayBoard. CreateDisplayWord " + newWord + "\n");
    20. }
    21.  
    22. public void UpdateDisplayWord(string originalWord, string newWord) {
    23.     displayWords = GetComponentsInChildren<DisplayWord>();
    24.     for (var i = 0; i < displayWords.Length; i++) {
    25.         var originalDisplayWord = displayWords[i];
    26.         // These return null ??
    27.         var displayButton = originalDisplayWord.GetComponentInChildren<DisplayButton>();
    28.         var displayButton2 = originalDisplayWord.gameObject.GetComponentInChildren<DisplayButton>();
    29.  
    30.         if (originalWord.Equals(displayButton.GetWord())) {
    31.             displayButton.SetWord(newWord);
    32.             originalDisplayWord.GetComponentInChildren<Button>().onClick.AddListener(() => OnButtonClick(newWord));
    33.             print("DisplayBoard. UpdateDisplayWord " + newWord + "\n");
    34.             break;
    35.         }
    36.  
    37.         print("DisplayBoard. UpdateDisplayWord " + newWord + "\n");
    38.     }
    39. }
    40.  
     
    Last edited: Apr 13, 2024
  2. baconrad

    baconrad

    Joined:
    Nov 27, 2023
    Posts:
    11
    Well, this is weird (when have we heard that before). I ran the exact same code and now it is working. Prior to that, I manually added the prefab to my hierarchy to see if it was a difference with instantiating. That worked which pointed to a problem with how I was instantiating. Then I removed it, put back the calls to instantiate and that worked as well. Really don't understand what is going on that would change the outcome.