Simple problem

Ok, so this is very simple. When I use this script in update, as usual, nothing wrong with it. Buuutt… its when the Replace(“”,“”) comes into update… it gets very weird. if you look at my script that converts words into another word:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class test : MonoBehaviour {

    public bool canChangeName = true;
    public bool checkforWord = false;
    public GameObject InputFields;
    public InputField User_Name;
    public Text text;

    public void Update()
        if (canChangeName)
            text.text = User_Name.text;
            checkforWord = true;
            if (checkforWord)
                text.text = User_Name.text.Replace("Lost Syndicate", "INVALID");
                text.text = User_Name.text.Replace("Lost_Syndicate", "INVALID");
                text.text = User_Name.text.Replace("Lost_Sndicate", "INVALID");
            if (Input.GetKeyDown(KeyCode.Return))
                text.text = User_Name.text;
                text.text = User_Name.text.Replace("Lost Syndicate", "INVALID");
                User_Name.enabled = false;
                canChangeName = false;


you can test it out and everything but what its supposed to do is go through those “words” and replace them with “INVALID”. Now the problem starts here. When i tested it out… only the last word said “INVALID” (in this case it was “Lost_Sndicate”) now i dont know how to fix this, i tried removing it from checkforword and putted it below text.text but ONLY the bottom text gets converted. Here is my example

text.text = User.text;
// here is where the problem starts.

text.text = User.text.Replace("alpha1","INVALID")    // doesnt work!
text.text = User.text.Replace("alpha2","INVALID")    // doesnt work!
text.text = User.text.Replace("alpha3","INVALID")    // doesnt work!
text.text = User.text.Replace("alpha4","INVALID")    // works!

// Them scrambled around:

text.text = User.text.Replace("alpha3","INVALID")    // doesnt work!
text.text = User.text.Replace("alpha4","INVALID")    // doesnt work!
text.text = User.text.Replace("alpha2","INVALID")    // doesnt work!
text.text = User.text.Replace("alpha1","INVALID")    // works!

it only checks the last one, and doesnt check the others besides that last one.
does anybody understand why this is happening?

User.text.Replace() doesn’t alter the contents of User.text, it just returns a new string. You’re only overwriting the contents of text.text each time, so only the final Replace call will actually do anything (and if the string you’re replacing isn’t in User.text, it’ll just put the User.text string unedited into text.text).

To do what it looks like you’re trying to do, use text.text = text.text.Replace().

Only the last one is shown because you’re overwriting text.text multiple times. It is working but each line replaces the previous one until the final call (which is the result you see)…

string.Replace returns source string in the case if were not any occurrences.

For example: User.text = "alpha1";

text.text = User.text.Replace("alpha1","INVALID")    // will be replaced by "INVALID", text.text== "INVALID",  User.text == "alpha1"
 text.text = User.text.Replace("alpha2","INVALID")    // text.text== User.text
 text.text = User.text.Replace("alpha3","INVALID")    // text.text== User.text
 text.text = User.text.Replace("alpha4","INVALID")    // text.text== User.text

You can use ‘switch’ operator.