I am trying to check if the value in string word from one class matches any element in the array stringAnswers in another and if it does i want the score to increment by 1. For some reason the code im using below increments the score by 1,2 and 3 depending on the word that is displayed any help would be awesome cheers.
public class Player : MonoBehaviour
{
public int Score = 0;
private string[] StringAns = {"steve", "grace", "lilly"};
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.Space))
{
RaycastHit hit;
if (Physics.Raycast(transform.position, transform.forward, out hit))
{
if (hit.transform.GetComponent<ButtonNo>() != null)
{
foreach (string stringAnsers in StringAns)
{
if (stringAnsers.Equals(FindObjectOfType<GameController>().word))
{
Debug.Log(" Button has been looked at");
FindObjectOfType<GameController>().RandText();
}
else
{
Debug.Log(" Button has been looked at");
FindObjectOfType<GameController>().RandText();
Score++;
}
}
}
}
if (Physics.Raycast(transform.position, transform.forward, out hit))
{
if (hit.transform.GetComponent<ButtonYes>() != null)
{
foreach (string stringAnsers in StringAns)
{
if (stringAnsers.Equals( FindObjectOfType<GameController>().word) )
{
Debug.Log(" Button has been looked at");
FindObjectOfType<GameController>().RandText();
Score++;
}
else
{
FindObjectOfType<GameController>().RandText();
}
}
}
}
}
}
}
GameController
public class GameController : MonoBehaviour
{
public TextMesh InfoText;
public TextMesh WallText;
public string word;
public Player player;
public string[] Strings = { "stev", "lilly", "grace" };
// Use this for initialization
void Start()
{
RandText();
}
// Update is called once per frame
void Update()
{
InfoText.text = "Is this Spelling Correct ?\n Score: " + player.Score;
}
public void RandText()
{
word = Strings[Random.Range(0, Strings.Length)];
WallText = GameObject.Find("WallText").GetComponent<TextMesh>();
WallText.text = word;
}
}