- Home /
Button not executing function. C#
I have a button that i would like to call a specific math function with. I placed Debug.Log messages to test what was going on. It appears that The button is not even calling the function because when i click it, the very first debug.log(3) for that function doesn't show up in the console.
Here is a screen shot and my script. I feel like i may have setup the script incorrectly because i am a newbie, but it should still call the Debug.Log(3) right?
I get Debugs 1 and 2 but not 3. I commented out the rest of my math function to test. Screenshot at bottom. using UnityEngine; using UnityEngine.UI; using System.Collections; using System;
public class Calculations : MonoBehaviour {
public GameObject textField_1;
public GameObject textField_2;
public GameObject textField_3;
public Text Result;
InputField t1;
InputField t2;
InputField t3;
void Start()
{
Debug.Log (1);
t1 = textField_1.GetComponent<InputField> ();
t2 = textField_2.GetComponent<InputField> ();
t3 = textField_3.GetComponent<InputField> ();
Debug.Log (2);
}
public void Product() {
Debug.Log (3);
double a = Convert.ToDouble(t1.text);
double b = Convert.ToDouble(t2.text);
double c = Convert.ToDouble(t3.text);
double d = a / 2;
//double e = d * d;
//double f = (e * 3.14) / b;
Result.text = d.ToString ();
}
}
UPDATE: I just created a new button and applied the calculations script to it and added everything in the inspector. I get the Debug.log(3) but I also get an error. Here it is
FormatException: Unknown char: r System.Double.Parse (System.String s, NumberStyles style, IFormatProvider provider) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Double.cs:209) System.Double.Parse (System.String s) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Double.cs:180) System.Convert.ToDouble (System.String value) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/Convert.cs:983) Calculations.Product () (at Assets/Script/Calculations.cs:28) UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:110) UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:574) UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:716) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44) UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52) UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269) UnityEngine.EventSystems.EventSystem:Update()
Is there something wrong with my function? Why doesnt my other button do this?
Thanks in advance for help!!!
Answer by Ian-McCleary · Apr 28, 2015 at 03:55 AM
So i figured out that there was most likely a scripting error on my part. I reverted back to an old version and everything is working again even though that part stays almost the exact same. I dont need help anymore and thanks to those who did!
Answer by Hrungdak · Apr 27, 2015 at 05:07 AM
Seems like your input value to the Convert.ToDouble-Method is wrong.
You can use TryParse to avoid this:
double a = 0;
if (!double.TryParse(t1.text, out a))
// t1.text can not be converted to double
Answer by Noob_Vulcan · Apr 27, 2015 at 05:17 AM
Looks like that the problem is with your Text Fields ...
Make sure that you Text Fields have Numeric input.
double a = Convert.ToDouble("any_string"); //Wrong ..
double a = Convert.ToDouble(9857); //Right
or
double a = Convert.ToDouble("3423"); //Right
Are those random numbers you put in there? or do they have to be specific?
Any random number..... Just dont but any alphabetical alphabetical character
Then how is it supposed to realize i want the numbers from the text boxes/input fields? It just used the numbers i put in the parentheses
Use Convert.ToDouble(t1.text)
Input a number in text box
haha yes i realize this, but it wasn't working. I reverted back to an old version of my script and caught myself up. I think that there was some sort of error that i was unaware of going on because the old version doesn't change at all in that section. Thanks for the help anyway!
Your answer
Follow this Question
Related Questions
Import Componet ( Variable ) Error Unexpected Token 3 Answers
C# script failing silently? 0 Answers
GooglePlayServices & GoogleMobileAds 0 Answers
How to get the colliders working? 1 Answer