I tried to make a simple game about word randomization, after I made a simple script from a tutorial I managed to make a random word, such as “words” to “wrdos” but how to make a random word in a sentence? I want to make a sentence “I am a teacher” to “teacher a i am”, here is an example of a script that I made in C #, what should I change to be able to randomize a sentence and not scramble a word
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class Word
{
public string word;
[Header("leave empty if you want randomize")]
public string desiredRandom;
public string GetString()
{
if (!string.IsNullOrEmpty(desiredRandom))
{
return desiredRandom;
}
string result = word;
result ="";
List<char> characters = new List<char>(word.ToCharArray());
while (characters.Count > 0)
{
int indexChar = Random.Range(0, characters.Count -1);
result += characters[indexChar];
characters.RemoveAt(indexChar);
}
return result;
}
}
public class WordScramble : MonoBehaviour {
public Word[] words;
[Header("UI Reference")]
public CharObject prefab;
public Transform container;
public float space;
public float lerpSpeed = 5;
List <CharObject> charObjects = new List<CharObject>();
CharObject firstSelected;
public int currentWord;
public static WordScramble main;
void Awake ()
{
main = this;
}
// Use this for initialization
void Start () {
ShowScramble(currentWord);
}
// Update is called once per frame
void Update () {
RepositionObject();
}
void RepositionObject()
{
if (charObjects.Count == 0)
{
return;
}
float center = (charObjects.Count - 1) / 2;
for (int i = 0; i < charObjects.Count; i++)
{
charObjects*.rectTransform.anchoredPosition*
= Vector2.Lerp(charObjects*.rectTransform.anchoredPosition,*
new Vector2((i - center) * space, 0), lerpSpeed * Time.deltaTime);
charObjects*.index = i;*
}
}
public void ShowScramble()
{
ShowScramble(Random.Range(0, words.Length - 1));
}
//
public void ShowScramble(int index)
{
charObjects.Clear();
foreach (Transform child in container)
{
Destroy(child.gameObject);
}
if (index > words.Length - 1)
{
Debug.LogError(“index out of range between 0-” + (words.Length -1).ToString());
return;
}
char[] chars = words[index].GetString().ToCharArray();
foreach (char c in chars)
{
CharObject clone = Instantiate(prefab.gameObject).GetComponent();
clone.transform.SetParent(container);
charObjects.Add(clone.Init(c));
}
currentWord = index;
}
public void Swap(int indexA, int indexB)
{
CharObject tmpA = charObjects[indexA];
charObjects[indexA] = charObjects[indexB];
charObjects[indexB] = tmpA;
charObjects[indexA].transform.SetAsLastSibling();
charObjects[indexB].transform.SetAsLastSibling();
CheckWord();
}
public void Select(CharObject charObject)
{
if (firstSelected)
{
Swap(firstSelected.index, charObject.index);
// unselect
//firstSelected = null;
firstSelected.Select();
charObject.Select();
}
else
{
firstSelected = charObject;
}
}
public void UnSelect()
{
firstSelected = null;
}
public void CheckWord()
{
StartCoroutine(CoCheckWord());
}
IEnumerator CoCheckWord()
{
yield return new WaitForSeconds(0.5f);
string word = “”;
foreach (CharObject charObject in charObjects)
{
word += charObject.character;
}
if (word == words[currentWord].word)
{
currentWord++;
ShowScramble(currentWord);
}
}
}
thanks for help