I am Trying to find what position a Space is in my Array and get them to a array of floats

I am making a guessing game and want to make the spaces spawn in as empty boxes where as the letters are spawned as boxes.
This code try’s to detect what letters in the string are spaces and then tell the array what they are and then add the position of the space to the array.
These are the Uisings–>

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

public string WordToGuess;
public float WordLength;
public float[] SpacesInWord;
public GameObject LetterPrefab;

void Start ()
{
    WordLength = WordToGuess.Length;
    SpacesInWord = new float[WordToGuess.IndexOf(" ")];
    for (int i = 0; i < WordToGuess.Length; i++)
    {
        GameObject Letter = Instantiate(LetterPrefab);
        Letter.transform.parent = this.transform;

        if (WordToGuess.Contains(" "))
        {
            SpacesInWord *= WordToGuess.IndexOf(" ");*

}
}

  • }*

  • void Update ()*
    {

  • }*
    }
    Thank you

Well, based on our talk, here is what i can offer, i added the check solution. You are probably checking in another class, but this is an example of how to do the check when using my solution :

    using System.Linq;
    using System.Collections.Generic;
    using UnityEngine;

    public class WordDisplay : MonoBehaviour
    {
        public bool WordGuessed;
        private string currentWord;
        public string WordToGuess;
        private List<Transform> letters;

        public GameObject LetterPrefab;
        public GameObject SpacePrefab;

        void Start()
        {
            currentWord = null;
            WordToGuess = null;
            letters = new List<Transform>();
            WordGuessed = false;
        }

        private void ResetLetters()
        {
            WordGuessed = false;
            transform.DetachChildren();
            Transform l;
            while (letters.Count > 0)
            {
                l = letters.First();
                letters.RemoveAt(0);
                Destroy(l);
            }
        }

        private void CreateWord()
        {
            char c;
            GameObject letter;
            for (int i = 0; i < WordToGuess.Length; i++)
            {
                c = WordToGuess*;*

if (c == ’ ')
{
letter = Instantiate(SpacePrefab);
}
else
{
letter = Instantiate(LetterPrefab);
}

letter.transform.parent = this.transform;
letters.Add(letter.transform);
}
}

void Update()
{
if (currentWord != WordToGuess)
{
ResetLetters();
CreateWord();
}
else
{
//If a new word was not set
//Some other logic can be performed here
//For example checking if the entered letters are correct
WordGuessed = CheckLetters();
}
}

private bool CheckLetters()
{
char userLetter;
char wordLetter;
for (int i = 0; i < letters.Count; i++)
{
wordLetter = WordToGuess*;*

//Ignore space characters
if (wordLetter == ’ ')
{
continue;
}

//Here you get the value of the letter in the box
//This example assumes there is a component containing the letter under the Value property
userLetter = letters*.gameObject.GetComponent().Value;*
//Stop at first error, no need to go further
if (wordLetter != userLetter)
{
return false;
}
}
return true;
}

//A separate method that can be set on a button
public void CheckWord()
{
WordGuessed = CheckLetters();
}
}
Hope this helps.

I think that the easiest option will be loop over the WordToGuess.

You can try this:

 public string WordToGuess;
 public float WordLength;
 public List<int> SpacesInWord; //indexes can be ints
 public List<Vector3> SpacesPosition;
 
 public GameObject LetterPrefab;
 void Start ()
 {
     WordLength = WordToGuess.Length;
     SpacesInWord = new List<int>();
	 SpacesPosition = new List<Vector3>();
	 
     for (int i = 0; i < WordToGuess.Length; i++)
     {
         GameObject Letter = Instantiate(LetterPrefab);
         Letter.transform.parent = this.transform;
         if (WordToGuess *== ' ')*

{
SpacesInWord.Add(i);

  •  	 SpacesPosition.Add(Letter.transform.position);*
    

}
}
}

But I guess I am missing the point of that.