I'm trying to create a falling word game like z-type (code here). Once the game starts a few words are displayed on the screen. When the user types a letter and if that matches with the first letter of any of the words displayed, an "activeWord" tag is added to the word. I have also created a laser script that checks if the tag is active and when that happens, it shoots the laser. What's happening right now is that the laser is shot only once i.e when the first letter matches but doesn't shoot a laser when the remaining words are typed. Can someone please have a look at the code and tell me where I'm making a mistake.
this is the word display script where the active word is assigned:
public void RemoveLetter() // remove the first letter if its correct and so on for the remaining letters. change the color of the word to red and add the "activeddWord" tag.
{
text.text = text.text.Remove(0, 1);
text.color = Color.red;
text.gameObject.tag = "activeWord";
}
public void RemoveWord()
{
Destroy(gameObject); // removes the word from display
}
here is the input manager script:
public class WordInput : MonoBehaviour {
public WordManager wordManager;
void Update () {
foreach (char letter in Input.inputString)
{
wordManager.TypeLetter(letter);
}
}
EDIT
This is the player script where the laser is instantiated:
public class Player : MonoBehaviour {
public GameObject laserPrefab;
void Start () {
transform.position = new Vector3(0.0f, -3.92f, 0); // player position
}
void Update () {
Instantiate(laserPrefab, transform.position, Quaternion.identity);
}
}
This is the updated laser script:
public class Laser : MonoBehaviour
{
public float speed = 10.0f;
private Vector3 laserTarget;
private void Start()
{
}
GameObject activeWord = GameObject.FindGameObjectWithTag("activeWord");
if (activeWord != null && activeWord.GetComponent<Text>().text.Length > 0){
laserTarget = activeWord.transform.position; // find position of word
transform.Translate(laserTarget * speed * Time.deltaTime);
for (int i = 0; i < Input.inputString.Length; i++){
Debug.Log("Character from input string: " + Input.inputString[i] + "\n" +
"Character from word displayed: " + activeWord.GetComponent<Text>().text[0]);
if (Input.inputString[i] == activeWord.GetComponent<Text>().text[0]){
laserTarget = activeWord.transform.position; // find position of word
transform.Translate(laserTarget * speed * Time.deltaTime); // shoot the laser
}
}
}
After I do this, there are two major errors that I get:
- As soon as I hit play, the laser fires continuously without stopping.
- The laser still shoots only once after the word is typed...This is the message I get in the console (screenshot attached). There is a difference of one word between the input string and whats displayed on the screen...Can someone please tell me how to rectify these.....