简体   繁体   中英

When comparing a users answer to randomly generated code, the program thinks that they are always the same no matter what is input

I am building a game that generates a random code for the user to remember and then asks them to input it back. When checking to see if the users input is correct, the script always thinks that the input was correct despite what I have inputed. I think it may be an issues as a result of passing variables incorrectly because the generated code variable is on a seperate script. I have tried multiple methods of comparing these scripts but no matter what I get the same result.

Here the random code is generated and I assign a variable to it to be called in another script.

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

public class RandomAlphaNumerical : MonoBehaviour
{
    public Text StringText;
    public string question;
    
    

   public void Start()
    {
        
     char[] letters = "qwertyuiopasdfghjklzxcvbnm1234567890!@#$%^&*()" .ToCharArray();
        System.Random r = new System.Random();
        string randomString = "";
        for (int i = 0; i < 6; i++)
        {
            randomString += letters[r.Next(0, 45)].ToString();
        }

        question = randomString;

        StringText.text = randomString;

    }   
}

Here is where the strings are compared. No matter what I try it will always change to scene 4 the 'win screen'.

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

public class Answer : MonoBehaviour
{
    public string input;
    public string correctprompt;

    

    public void Start()
    {
        GameObject.Find("RandomAlphanumerical").GetComponent<RandomAlphaNumerical>().question = correctprompt;

    }

    public void ReadStringInput(string s)
    {
        input = s;

        Console.WriteLine(input);
        
        

      if (input == correctprompt)
        {
            SceneManager.LoadScene(4);
        }

      if (input != correctprompt)
        {
            SceneManager.LoadScene(5);
        }


    }
}

I am worried that the issue may also be in the unity gameobjects so if there is an effective way to do this with worrying about gameobjects for each script such that would be the best. However I really will take any advice you have, Im completely stuck.

Thanks for reading, any help will be greatly appreciated.

PS Please comment if there is anymore infomation you might need to solve this issue.

You are setting question be equal to correctanswer

GameObject.Find("RandomAlphanumerical").GetComponent<RandomAlphaNumerical>().question = correctprompt;

But you want it to be the other way around instead.

correctprompt = GameObject.Find("RandomAlphanumerical").GetComponent<RandomAlphaNumerical>().question = correctprompt;

It's also a good idea to grab the value when you need it. Since now you are both generating it and storing it in separate Start() and you don't know the order order in which these are executed.

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

public class Answer : MonoBehaviour
{
    public string input;
    //public string correctprompt;
    
    private RandomAlphaNumerical _randomAlphaNumerical;

    public void Start()
    {
        // Get a reference to the script with the random code
        _randomAlphaNumerical = GameObject.Find("RandomAlphanumerical").GetComponent<RandomAlphaNumerical>();

    }

    public void ReadStringInput(string s)
    {
        input = s;
        // Grab the value here instead
        string correctprompt = _randomAlphaNumerical.question;
        
        // Log both values to see what they are
        Console.WriteLine("Input: " + input);
        Console.WriteLine("Correct answer: " + correctprompt);           

        if (input == correctprompt)
        {
            SceneManager.LoadScene(4);
        }

        if (input != correctprompt)
        {
            SceneManager.LoadScene(5);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM