FormatException: Input string was not in a correct format.

Hello!
I’m afraid I’m a beginner when it comes to coding and scripting, but sadly my college makes us make games, even without knowing how to code!
I borrowed some code from an upperclassman, which should technically work since it’s almost the same thing (only the model used is different)

This is my error:

FormatException: Input string was not in a correct format.
System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) (at <7d97106330684add86d080ecf65bfe69>:0)
System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) (at <7d97106330684add86d080ecf65bfe69>:0)
System.Int32.Parse (System.String s) (at <7d97106330684add86d080ecf65bfe69>:0)
RaycastBehavior.Update () (at Assets/Scripts/RaycastBehavior.cs:81)

Now I understand that this error is in line 81 (here:80) of my script, but I don’t know how to fix it. Googling has told me to use TryParse, but I don’t understand how to use it.

Here’s the code:

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

public class RaycastBehavior : MonoBehaviour
{
    //the camera from which we shoot the rays
    private Camera mainCamera;

    //the displayed text
    public Text tipText;
    public Text taskText;
    public Text failText;

    //the failure and the tip counter
    private int failCount = 0;
    private int tipCount = 0;

    //the current step and the game object assosciated with it
    private int currentStep = 0;
    private GameObject currentTaskObject;

    //the layer which contains the clickable items
    private LayerMask mask;

    //the object which can be highlighted by clicking on the help butoon
    private GameObject highlightedObject;

    //the highlighting material
    public Material highlighter;

    //checking if the object is currently highlighted or not
    bool objectHighlighted;

    //Start is called before the first frame update
    private void Start()
    {
        //saving the Main Camera of the scene in a variable
        mainCamera = GameObject.Find("Main Camera").GetComponent<Camera>();

        //saving the layer in a variable
        mask = LayerMask.GetMask("ActionLayer");

        SetNewStatus();

        ChangeTask();

        //the Object assosciated with the current task
        currentTaskObject = GameObject.FindGameObjectWithTag(currentStep.ToString());

        //displaying the text
        tipText.text = "Hilfe: " + tipCount.ToString();

        failText.text = "Fehler: " + failCount.ToString();

    }

    //Update is called once per frame
    void Update()
    {
        if(currentStep == 6)
        {
            //SceneManager.LoadScene("Main_Menu");
            taskText.text = "Training beendet! Sie haben " + failCount.ToString() + " Fehler gemacht und " + tipCount.ToString() + " Mal die Hilfe beansprucht.";
           
        }

        if(Input.GetMouseButtonDown(0) && currentStep < 6)
        {
            //casting the ray to where the cursor is
            Ray ray = mainCamera.ScreenPointToRay(Input.mousePosition);
           
            //stores what is hit by the ray
            RaycastHit hit;

            //when something gets hit by a ray
            if(Physics.Raycast(ray, out hit, 100f, mask))
            {
                if(currentStep == int.Parse(hit.transform.tag))
                {
                    if(highlightedObject == currentTaskObject)
                    {   
                        //changing the color back to normal
                        string mName = currentTaskObject.name;
                        highlightedObject.GetComponent<Renderer>().material = Resources.Load<Material>("Materials/" + mName); 
                        Debug.Log(currentStep);
                        objectHighlighted = false;
                        SetNewStatus();
                        ChangeTask();
                    }
                    else
                    {

                       
                        Debug.Log(currentStep);
                        SetNewStatus();
                        ChangeTask();
                    }
                }
                //clicking on the gameboy changes nothing
                else if(hit.transform.tag == "-1")
                {
                    Debug.Log("GameBoy");
                }
                //clicking on the wrong object raises the failure count
                else if (currentStep != int.Parse(hit.transform.tag))
                {
                    RaiseFailure();
                }
            }
        }
    }

    //changing the text and going over to the next task
    void SetNewStatus()
    {
        //going over to the next step
        currentStep++;
        currentTaskObject = GameObject.FindGameObjectWithTag(currentStep.ToString());

        //!!!!!!!!!!!!!!!!!!!!!!!!!könnte raus!!!!!!!!!!!!!!
        taskText.text = currentStep.ToString();
    }

    //increases the amount of failures by 1
    void RaiseFailure()
    {   
        failCount++;
        failText.text = "Fehler: " + failCount.ToString();
    }

    public void HighlightTip()
    {
        //saving the currentStep as the object which should be highlighted
        highlightedObject = GameObject.FindGameObjectWithTag(currentStep.ToString());

        //highlighting the object by assigning a material
        highlightedObject.GetComponent<Renderer>().material = highlighter;

        //increasing the tip-counter and displaying it
        if(objectHighlighted == false)
        {
        tipCount++;
        tipText.text = "Hilfe " + tipCount.ToString();
        Debug.Log(highlightedObject);
        }

        objectHighlighted = true;

    }

    //changing the task
    void ChangeTask()
    {
        switch (currentStep)
        {
            case 1:
                taskText.text = "Schritt 1/5: Wechsle die Batterie aus.";
                break;

            case 2:
                taskText.text = "Schritt 2/5: Lege ein neues Spiel ein.";
                break;

            case 3:
                taskText.text = "Schritt 3/5: Schalte den Gameboy ein.";
                break;

            case 4:
                taskText.text = "Schritt 4/5: Drehe die Lautstärke auf.";
                break;

            case 5:
                taskText.text = "Schritt 5/5: Drücke einen beliebigen Knopf, um das Spiel zu starten.";
                break;

        }
    }
}

I’d appreciate any help, thank you!

So, here’s the deal. TryParse is designed to try and parse the string into an int (in your case), but will return a bool if it’s successful or not. If you have tags that are not able to be converted into numbers, then tryParse is good to use and you can Google it to know how to use it.

The issue you are running into deals with your string not being in the correct format.

So, for example, if you pass in the string “layer 1”, int.Parse has no way to turn that into a number, because it doesn’t understand that.

But if I pass you the string “1”, then it can convert that to a number. What does this mean? If the code above was copied, then you missed the other key step which was setting up the tag properly.

So you’ll need to setup your tags to be numbers. However, I couldn’t tell you what numbers to use as I don’t know what the numbers should be.

You can actually see this in the code where there is a check if the tag is “-1”, which would be a valid parse.

Thank you for the reply!!
The tag on the object is on -1, I’m working on this project with my friend and she fixed up the tags. I checked them again because I thought that would be a problem!
I’ll check again tomorrow, it’s sorta late already

I doubt a college would “make you make games” without the necessary skills. What class is this? And you “borrowed” code for an assignment? There’s a term for that…

1 Like

The class is called Virtual Systems. And my upperclassman gave me the code. We had some Unity workshops but that doesn’t really help teach this sorta coding and a 1-2 month timeframe

1 Like