- Home /
"The name 'Convert' does not exist in the current context" C#
I am trying to use Input Field boxes for multiplying the given input from the user. I need to change the string into an integer form so i can multiply it, then i need change it back to a string once it is multiplied so the answer can be displayed. (or at least i think that's how it should work)
I have 2 questions, How can i fix this error: Assets/Calculations.cs(20,25): error CS0103: The name `Convert' does not exist in the current context
and 2, how can i call from my own Input Field boxes instead of what i have in the example script? The script i have is a very rough idea of what i need to do.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Calculations : MonoBehaviour {
public InputField Field1;
public InputField Field2;
public Text Result;
// public void Sum() {
// int a = Convert.ToInt32(Field1.text);
// int b = Convert.ToInt32(Field2.text);
// int c = a+b;
// Result.text = c.ToString();
// }
public void Product() {
int a = Convert.ToInt32(Field1.text);
int b = Convert.ToInt32(Field2.text);
int c = a*b;
Result.text = c.ToString();
}
}
Answer by Addyarb · Mar 27, 2015 at 05:51 AM
As the other answer reads, put
using System;
At the top.
You did well with using UnityEngine.UI;
In order to get your text fields' data in, try this:
//Top of the script
public GameObject textField_1;
public GameObject textField_2; //drag these game objects into the script via the inspector.
InputField t1;
InputField t2;
void Start()
{
t1 = textField_1.GetComponent<InputField>();
t1 = textField_2.GetComponent<InputField>();
}
public void Product() {
int a = Convert.ToInt32(t1.text);
int b = Convert.ToInt32(t2.text);
int c = a*b;
Result.text = c.ToString();
}
So far your code works, except now it says it doesnt recognize "Result" in the current context. any ideas?
Sorry, never $$anonymous$$d i found a typo. Thank you for the help!!!
Answer by HarshadK · Mar 27, 2015 at 05:43 AM
Convert exists in System namespace so you either have to call it using:
System.Convert.ToInt32
or import the namespace with:
using System;
at the top.