Localization issue: "Could not evaluate the selector "1""

I am currently making a simple game and have been fidddling around with localizing my really simple UI pieces with Smart Strings. I am trying to add a localization for a simple line with a line like “Capacity: {currentvalue} / {maxValue}” and one for “Points: {points}”

In my code, I have a method to handle the smart string:

void UpdateStomachCapacityUI()
    {
        // Set parameters for the localized string for stomach capacity
        localizedCapacityString.Arguments = new object[] { capacity, maxCapacity };

        // Fetch and apply the localized string to the stomach capacity UI text
        localizedCapacityString.StringChanged += (localizedText) =>
        {
            stomachCapacityText.text = localizedText;
        };
        localizedCapacityString.RefreshString();
    }


    void UpdatePointsUI()
    {
        // Set parameters for the localized string for points
        localizedPointsString.Arguments = new object[] { points };

        // Fetch and apply the localized string to the points UI text
        localizedPointsString.StringChanged += (localizedText) =>
        {
            pointsText.text = localizedText;
        };
        localizedPointsString.RefreshString();
    }

My localization table is as follows:

where in the first key, the {0} and {1} are supposed to reference the two values from
localizedCapacityString.Arguments = new object[ ] { capacity, maxCapacity };
in my above code.

However, I receive an error when running my preview that says:
FormattingException: Error parsing format string: Could not evaluate the selector “1” at 23
Stomach Capacity: {0}/{1}
-------------------------^

It only seems to give this error when I add the {1} indicator, removing it causes the smart string to work properly (albeit not as I want it) and the Points code localizes correctly as well. I’ve tried to find some solutions for this and tinkered around a little, but couldn’t find anything and am not sure why it’s having this error.

Any ideas on how to fix it?

Thanks!

Nothing stands out in the code. Can you share an example project so I can take a closer look?

Of course, how should I go about that? Apologies, still a bit new to some of this.

You can upload it to something like Google Drive and share the link here or DM it to me. Alternatively, I can provide an FTP link to upload it.

There are a few issues here.

You have added a LocalizedStringEvent to both the Text GameObjects but you are using a separate script to update the text. That makes the 2 LocalizedStringEvents redundant, they are also whats causing the error as they don’t have values for the {0] and {1} placeholders. You can remove them.

There are also some performance issues in your scripts.
You are updating every frame, however you are subscribing to the change event every update.

localizedPointsString.StringChanged += (localizedText) =>
{
    pointsText.text = localizedText;
};

This means you add a new subscriber to the event each time. This will degrade performance over time as each update will call all the subscribers, essentially calling set text a lot of times.

Something like this will work better:

void UpdateStomachCapacityUI()
{
    // Set parameters for the localized string for stomach capacity
    localizedCapacityString.Arguments = new object[] { capacity, maxCapacity };
    stomachCapacityText.text = localizedCapacityString.GetLocalizedString();   
}

void UpdatePointsUI()
{
    // Set parameters for the localized string for points
    localizedPointsString.Arguments = new object[] { points };
    pointsText.text = localizedPointsString.GetLocalizedString();
}

Finally, I would suggest only running these methods when the value has changed as doing new object[] each update will be allocating memory each time that can cause performance issues. Once you have assigned the array you can change the value, you don’t need to assign a new array each time.
Something like this could work:

void UpdateStomachCapacityUI()
{
    // Set parameters for the localized string for stomach capacity
    localizedCapacityString.Arguments ??= new object[2];
    localizedCapacityString.Arguments[0] = capacity;
    localizedCapacityString.Arguments[1] = maxCapacity;
    stomachCapacityText.text = localizedCapacityString.GetLocalizedString();   
}

void UpdatePointsUI()
{
    // Set parameters for the localized string for points
    localizedPointsString.Arguments ??= new object[1];
    localizedCapacityString.Arguments[0] = points;
    pointsText.text = localizedPointsString.GetLocalizedString();
}

I would also look at adding some early return if the value does not change.

Something like:

int lastPointsUpdate = -1;

void UpdatePointsUI()
{
    if (lastPointsUpdate == points)
        return;

    lastPointsUpdate = points;
    
    // Set parameters for the localized string for points
    localizedPointsString.Arguments ??= new object[1];
    localizedCapacityString.Arguments[0] = points;
    pointsText.text = localizedPointsString.GetLocalizedString();
}

Thank you so much! Disabling the two Localize String Events was all I needed to do. Thanks as well for the code tips, much appreciated!