String only holds its value for one call

Hi! I am trying to create a prompt for picking up an item. In the game I am making, you pick up an item by dragging it onto your inventory, which calls the SwapBoxPrompt() function.

    GameObject AreYouSureTextGO;
    Text AreYouSureText;
    public string newitemstring;


    private void Awake()
    {
        
        AreYouSureTextGO = GameObject.FindWithTag("AreYouSureText");
        AreYouSureText = AreYouSureTextGO.GetComponent<Text>();
    }
    
    
    void SwapBoxPrompt()
    {
            AreYouSureText.text = "Are you sure you want to pick up a " + newitemstring + "?";
        }
    public void YesIWantSwap()
    {
            AreYouSureText.text = "You now are in possession of a " + newitemstring + ".";
      
    }

However, my issue is that after using newitemstring as part of the dialogue for SwapBoxPrompt, the next function that calls, YesIWantSwap, when you click the “Yes” button, will just say, “You are now in possession of a .”, totally ignoring the newitemstring. I want newitemstring to keep the same value as I have set it in the inspector, as any future calls will treat it as if nothing is written, even though in the inspector it still holds the value I wrote (It still says “Red Box”) but any calls to write newitemstring just return nothing. Thank you for your time!

Does the first question return the string correctly?

There are several scenarios in which a public Unity variable like 'newitemstring ’ can become empty:

  1. You are instantiating a prefab but in the prefab the string is left as empty. Sometimes developers or artists set the value of a public variable of a script that is attached to an instance of a prefab (i…e gameObject) in the scene but forgot to set the same variable in the corresponding prefab. Here in you case, maybe the value of ‘newitemstring’ is set in the gameObject but not its prefab. In this case, we you instantiate the prefab during the runtime, the ‘newitemstring’ ends up being empty.
  2. Somewhere in the code (in the same script or another script) is overwriting 'newitemstring ’ with empty string.
  3. Are you sure that this specific script is actually doing what you see in the game? Sometimes the developer mistakenly, attach the same script to two or more gameObjects without him/her knowing that. Then, one of those attached script (that actually displays the text in the game) has an empty value for this string. Make sure this is not your case.