- Home /
This question was
closed Oct 07, 2019 at 09:08 AM by
MOJAJA for the following reason:
used another way to solve
Word Spelling game ,how to add the letter to the word when click the button?
Hi all , I'm making a word spelling game , every words will have two blank. And there have four buttons that you can choose. When click the button will add the correct or wrong letter to the word. How to add a letter at a time and add a letter to the word ?
Like this:
Letter.CS:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Letter : MonoBehaviour
{
public char character;
public RectTransform rectTransform;
public Text text;
public int index;
public Letter Add(char c)
{
character = c;
text.text = c.ToString();
gameObject.SetActive(true);
return this;
}
}
SpellGame.CS:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SpellGame: MonoBehaviour
{
private DataCon dataCon;
public char[] arr;
public float space;
public float lerpSpeed=5;
public Transform parent;
public Letter prefab;
List<Letter> charObjects = new List<Letter>();
public int currentWord;
void Start()
{
dataCon = FindObjectOfType<DataCon>();
ShowTo(currentWord);
}
public void ShowTo(int index)
{
arr =dataCon.allQuiz[0].questions[Random.Range(0,dataCon.allQuiz[0].questions.Length)].queText.ToCharArray();
for (int i = 0; i < 2; i++)
{
arr[Random.Range(0,arr.Length)] = '_';
}
foreach (char c in arr)
{
Letter clone = Instantiate(prefab.gameObject).GetComponent<Letter>();
clone.transform.SetParent(parent);
charObjects.Add(clone.Add(c));
}
currentWord = index;
}
void RepositionObject()
{
if (charObjects.Count == 0)
{
return;
}
float center = (charObjects.Count - 0.9f) / 2;
for (int i = 0; i < charObjects.Count; i++)
{
charObjects[i].rectTransform.anchoredPosition = Vector2.Lerp(charObjects[i].rectTransform.anchoredPosition, new Vector2((i - center) * space, 0), lerpSpeed * Time.deltaTime);
charObjects[i].index = i;
}
}
void Update()
{
RepositionObject();
}
}
AddButton.CS:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AddButton: MonoBehaviour
{
public Text text;
private WordGame wordGame;
private Letter letter;
private void Start()
{
wordGame = FindObjectOfType<WordGame>();
letter = FindObjectOfType<Letter>();
}
public void ClickButton()
{
char[] c = text.text.ToCharArray();
for (int i = 0; i < wordGame.arr.Length; i++)
{
if (wordGame.arr[i]=='_')
{
wordGame.arr[i] = c[0];
letter.character = c[0];
}
}
}
}
01.png
(9.4 kB)
Comment
Answer by rahul_95 · Oct 01, 2019 at 09:50 AM
If i got your question correct ,you are asking a way to put a letter at a certain place in a string if thats is the question ,have a script which holds the char index and the char value ,and attach it to the button,now every button has data of which index in that string can be set and know the value also
public char charToPutInAString;
public int indexOfChar;
public void OnButtonClicked()
{
//first convert the string to char array
//set the stringArray[indexOfChar]=charToPutInAString;
}