"Cannot implicitly convert type 'int' to ' string'"

When typing out my bool and if statements, i simply get the error:

“Cannot implicitly convert type ‘int’ to ’ string’”

with the level:

level = int.Parse(input);

then i get the same error with each of my case numbers. Any ideas why it is doing this?

Hi Jon,

Can you copy/paste your full script, and apply code formatting to it please.


See also;

using System.Collections.Generic;
using UnityEngine;

public class Hacker : MonoBehaviour {

    //Game Configuration Data.
    string[] level1Passwords = { "doughnut", "cops", "patrol vehicle", "password" };
    string[] level2Passwords = { "federal", "virginia", "narcotics", "moulder and scully" };
    string[] level3Passwords = { "hacking", "spying", "edward snowden", "agency" };

    //Game State.
    string level; //game state
    enum Screen { MainMenu, Password, Win };
    Screen currentScreen;
    string password;

    //text delay for win screen.
    IEnumerator LevelWin()  
    {
        yield return new WaitForSeconds(1f);
        Terminal.WriteLine("Mainframe successfully hacked");
        Terminal.WriteLine("downloading files...");
        yield return new WaitForSeconds(1f);
        Terminal.WriteLine("25%");
        yield return new WaitForSeconds(1f);
        Terminal.WriteLine("50%");
        yield return new WaitForSeconds(1f);
        Terminal.WriteLine("75%");
        yield return new WaitForSeconds(1f);
        Terminal.WriteLine("100%!");
    }

    //Start Menu.
    void Start()
    {
        ShowMainMenu();
    }
    void ShowMainMenu()
    {
        currentScreen = Screen.MainMenu;
        Terminal.ClearScreen();
        Terminal.WriteLine("Welcome...");
        Terminal.WriteLine("There isn't much time,");
        Terminal.WriteLine("you must hack these");
        Terminal.WriteLine("secure systems to learn");
        Terminal.WriteLine("more about the Truth.");
        Terminal.WriteLine("Hack the NYPD");
        Terminal.WriteLine("Hack the FBI");
        Terminal.WriteLine("Hack the NSA");
        Terminal.WriteLine("Make your choice.");
    }

    //Screen designator.
    void OnUserInput(string input)
    {
        if (input.ToLower() == "menu")
        {
            ShowMainMenu();
        }
       else if (currentScreen == Screen.MainMenu)
        {
            RunMainMenu(input);
        }
        else if (currentScreen == Screen.Password)
        {
            CheckPassword(input);
        }
    }

    //Input control for main menu.
    void RunMainMenu(string input)
    {
        bool isValidLevelNumber = (input == "1" || input == "2");
        if (isValidLevelNumber)
        {
            level = int.Parse(input);
            StartGame();
        }
        else if (input.ToLower() == "exit")
        {
            Terminal.WriteLine("There is no exit, without Truth");
        }
        else
        {
            Terminal.WriteLine("Response Invalid");
        }
    }

    //Password screen.
    void StartGame()
    {
        currentScreen = Screen.Password;
        Terminal.ClearScreen();
        switch (level)
        {
            case 1:
                password = level1Passwords[0];
                break;
            case 2:
                password = level2Passwords[0];
                break;
            default:
                Debug.LogError("Invalid Mainframe");
                break;
        }
        string mainframemessage = string.Format("Entering {0} mainframe.", level);
        Terminal.WriteLine(mainframemessage);
    }

    //Password verification.
    void CheckPassword(string input)
    {
        if (input == password)
        {
            Terminal.WriteLine("Password Accepted.");
            StartCoroutine(LevelWin());
            string downloadmessage = string.Format("Downloading {0} Databanks.", level);
            Terminal.WriteLine(downloadmessage);
        }
        else
        {
            Terminal.WriteLine("Incorrect Password");
            Terminal.WriteLine("Please Enter Correct Password");
        }
    }
}

Thank you Jon,

Ok, so you have level declared at the top of your script as a string. The following line;

level = int.Parse(input);

…tries to parse the value of input and where possible, returns an int. So here is the issue, you are trying to put a type of int into a variable of type of string.

You have some choices. You could keep level as a string and change that line to this;

level = int.Parse(input).ToString();

The error will go away, however you may then want to address the switch statement and consider putting quotation marks around each of the cases, e.g. “1”, “2” and so on. Sometimes you can get away without any specific casting with ints and strings, but not always.

An alternative would be to set level to be an int in your declaration at the top, you can then leave the switch statement as-is and there will be no need for the .ToString().

Probably best to go back a bit in this lecture and see what level is supposed to be declared as.

Hope this helps :slight_smile:

Okay i think i understand, but im not sure how to change the whole lot over. i now have the inputs as “nypd”, “fbi” and “nsa”, and have made them the case numbers too, in quotation marks. but when i play the game and type in the level name “nypd” for example it just spits my word back at me, what have i missed?

Could you copy/paste the code back up for me again, with the changes.

never mind! i have solved it!
i was apparently trying to convert a string into a string!

1 Like

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

Privacy & Terms