- Home /
The question is answered, right answer was accepted
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[i] = WordToGuess.IndexOf(" ");
}
}
}
void Update ()
{
}
} Thank you
Can you explain the guessing game a bit more? I think there may be a better solution to your problem. Sonky108 gave a solution based on your code, but there might be other soltuions depending on what you are trying to achieve.
thank you for replying and this is what i want it to look like so when their is a space it is a different type of box. this is so that i can make levels for the guessing game very quickly and only need to type the word in and it is done rather than having to make many UI changes as i muse the Grid Layout tool this makes it faster thank you. pleas tell me if you want more explanation
public string WordToGuess;
public float WordLength;
public List<int> SpacesInWord; //indexes can be ints
public List<Vector3> SpacesPosition;
public GameObject LetterPrefab;
public GameObject SpacePrefab;
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[i] == ' ')
{
SpacesInWord.Add(i);
SpacesPosition.Add(Letter.transform.position);
}
}
for (int t = 0; t < SpacesPosition.Count; t++)
{
GameObject Space = Instantiate(SpacePrefab);
Space.transform.parent = this.transform;
}
}
void Update ()
{
}
}
Sorry, i wasn't clear enough. I understand the code you provided. What i don't understand is how your game is supposed to work :
You use variables with names like Word, but a word doesn't have spaces in it, otherwise it's a phrase.
What do spaces do in your game, not code-side, but in the game-play. I've done some simple games, like the hanged man game, where you have to guess a letter used in the hidden word/phrase and if it is in, it is shown where, otherwise a miss mark is added, once you run out of marks, you lose. This seems close ot it, but the code does unnecessary calculations. Which is why i'm trying to understand why you would need a space prefab. Is you are using a letter prefab, that means you can use alphanumeric characters on the prefab, why would you not be able to use the " " character with the letter prefab, ins$$anonymous$$d of using a space prefab.
You talked about levels, but in a word guessing game, levels differ only by word/phrase and the difficulty to guess them. Does that mean you are making a game that has other types of guessing than words? Does that mean that you use those prefabs in those other types of games too?
Since there is not enough information, the following is a guess, correct me if i'm wrong :
You first choose a word/phrase
You replace letters with spaces (number of spaces depends on difficulty)
You display the result with spaces
The player has to guess the word/phrase by typing in the whole word/phrase in a n answer block, or filling in the space blocks
There is probably a limited number of errors leading to game over
If this is mostly right, i can suggest a few improvements, otherwise you can either use the anwer from Sonky108 or we can try to improve the code with a more detailed explanation.
Answer by Sonky108 · Jul 26, 2018 at 09:08 AM
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[i] == ' ')
{
SpacesInWord.Add(i);
SpacesPosition.Add(Letter.transform.position);
}
}
}
But I guess I am missing the point of that.
Answer by madks13 · Jul 26, 2018 at 12:11 PM
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[i];
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[i];
//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[i].gameObject.GetComponent<SomeClass>().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.
This Dose work but it is a big and complex way so sorry im giving it to the simple method so if anyone looks they can have a small amount of text but thank you for putting in so much effort!