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!