# Simple problem (203874)

**URL:** https://discussions.unity.com/t/simple-problem-203874/203874
**Category:** Questions & Answers
**Created:** [February 18, 2018, 8:46pm UTC](https://discussions.unity.com/t/simple-problem-203874/203874 "2018-02-18T20:46:51Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Lost\_Syndicate](https://avatars.discourse-cdn.com/v4/letter/l/35a633/32.png) [@Lost\_Syndicate](https://discussions.unity.com/u/Lost_Syndicate)
#### Post date: [February 18, 2018, 8:46pm UTC](https://discussions.unity.com/t/simple-problem-203874/203874/1 "2018-02-18T20:46:51Z")

</div>

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;
                InputFields.SetActive(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?

---

<div class="post-metadata">

### Author: ![Commoble](https://avatars.discourse-cdn.com/v4/letter/c/77aa72/32.png) [@Commoble](https://discussions.unity.com/u/Commoble)
#### Post date: [February 19, 2018, 6:15am UTC](https://discussions.unity.com/t/simple-problem-203874/203874/2 "2018-02-19T06:15:47Z")

</div>

`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()`.

---

<div class="post-metadata">

### Author: ![ransomink](https://avatars.discourse-cdn.com/v4/letter/r/b5ac83/32.png) [@ransomink](https://discussions.unity.com/u/ransomink)
#### Post date: [February 19, 2018, 8:31am UTC](https://discussions.unity.com/t/simple-problem-203874/203874/3 "2018-02-19T08:31:52Z")

</div>

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)…

---

<div class="post-metadata">

### Author: ![dmitriy-untilov](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@dmitriy-untilov](https://discussions.unity.com/u/dmitriy-untilov)
#### Post date: [February 19, 2018, 1:38pm UTC](https://discussions.unity.com/t/simple-problem-203874/203874/4 "2018-02-19T13:38:11Z")

</div>

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.
