Random.Range - namespace clashes

using UnityEngine;
using System; 

public class Hacker : MonoBehaviour
{   //Game config data
    string[] level1Passwords = { "apple", "class", "lunch", "books", "teacher","homework" };
    string[] level2Passwords = { "documents", "shredder", "meeting", "company", "computers", "copier" };
    //Game State
    int level;
    enum Screen { ShowMenu, Password, Win };
    Screen currentScreen;
    string password;

    void Start() //Use this for initialization
    {
        print(level1Passwords[0]);
        ShowMenu(); //statement
    }
    void ShowMenu ()
    {   currentScreen = Screen.ShowMenu; 
        Terminal.ClearScreen();//clears the screen
        Terminal.WriteLine("There are three systems you can hack"); //prints to the terminal
        Terminal.WriteLine("1.School Terminal");
        Terminal.WriteLine("2.Company Terminal");
        Terminal.WriteLine("3.Bank Terminal");
        Terminal.WriteLine("Enter your selection: ");
    }
    void OnUserInput(string input)
    {
        if (input == "menu") // prints user input on console; Boolean
        {
            ShowMenu();

        }
                else if (currentScreen == Screen.ShowMenu)
        {
                RunMain(input);
        }
        else if (currentScreen == Screen.Password)
        {
            CheckPassword(input);
        }

    }

     void RunMain(string input)
    {
        bool isValidLevelNumber = (input == "1" || input == "2");
        if (isValidLevelNumber)
        {
            level = int.Parse(input);
            StartGame();
        }
        else if (input == "Crimson")
        {
            Terminal.WriteLine("Welcome back Crimson"); // Easter Egg
        }
        else
        {
            Terminal.WriteLine("Please choose a Valid level!"); // Incorrect level choice
        }

    }

    void StartGame()
    {
        currentScreen = Screen.Password;
        print(level1Passwords.Length);
        print(level2Passwords.Length);
        Terminal.ClearScreen();
        switch(level)
        {
            
            case 1:
                int index = Random.Range(0, level1Passwords);
                password = level1Passwords[index];
                break;
            case 2:
                
                password = level2Passwords[0];
                break;                                                                                                                         
            default:
                Debug.LogError("Invalid Level Selection!");
                break;

        }

        Terminal.WriteLine("Please enter your password: ");
    }
    void CheckPassword(string input)
    {
        if (input == password)
        {
            Terminal.WriteLine("Correct !"); 

        }
        else
        {
            Terminal.WriteLine("Incorrect !"); 
        }
    }
}

If I keep using System and using Unity the Random in Random.Range goes blue. If I take out using system, the Random is not blue and i get cannot convert string .

EDIT: found the error problem, but Random.Range still does not turn blue

Hi,

In your switch statement you are using level1Passwords, but this is an array and Random.Range requires either an int or float.

Add .Length to level1Passwords and this will return the size of the array.

Hope this helps.

Thank you it did, why does the RandomRange not go Blue?

I’m not entirely sure what you mean by blue, do you mean syntax highlighting?

when you start typing for example “case” when you type it in between switch, it will go blue or as soon as you start typing, it will auto fill, random does not do that. When it should

Try preceding it with UnityEngine, for example;

int index = UnityEngine.Random.Range(0, level1Passwords);

Perfect thank you soo much

1 Like

No problem.

The reason it wasn’t highlighted is because Random.Range appears in both System and UnityEngine namspaces.

By preceding the class name with the namespace you are specifically stating which you are referring to.

Also, when you copy/paste code into the forum, if you add the code formatting characters before and after it the code will be formatted and easier to read.


See also;

That makes since

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms