How to assign a value to buttons, every button would multiply each other and update a text
Hi,
I'm brand new to scripting and I have tried for couple of weeks to get this figured out but everything I have tried (using pre-made calculator scripts, trying to write my own through tutorials) has failed. I'm under a deadline, but can't figure out for the life of me how I could set three buttons, have it assigned a value to update a text showing the result of the multiplication. Let me explain.
So what I'm trying to do is to select a button (called initial) and assign a value of let's say 100 and by clicking another button (called multiplier 1) and having assigned a value of 3, it would update a text value (called result) showing the result of the multiplication. I would also like to have a third button (called multiplier 2) multiply the result of the previous multiplication and so on.
If there is anyone out there that could help out with a basic script (yea I know that this is basic scripting), I would really appreciate it.
Answer by ElijahShadbolt · Nov 13, 2016 at 08:38 PM
You could use Unity's newer UI system's Text and Button components to display the value on screen.
Create a Text label via the menu. GameObject > UI > Text. This should also create a Canvas. This text object will display our calculated value.
Create a Calculator script (CalculatorScript.cs below) and add it as a component to the Text, or another important GameObject.
In the inspector, assign the Text component as its 'Result Text'.
If the 'Result Value' is 0, multiplying will have no effect, so set it to something else, like 1.
Create a Button via the menu by selecting GameObject > UI > Button. Move it around in the scene so that you can still see the Result Text. Or you could Right-Click the Canvas in the Hierarchy and select UI > Button. You can add as many as you like. If you want, you can change the label of a button by going to its child Text GameObject and editing the textbox in the Text component.
Add a listener to each button's OnClick event. Select a Button in the Hierarchy and under OnClick() click the little '+' button to add a listener. Where it says 'None (objec...' assign the GameObject that holds the CalculatorScript. Where it says 'No Function', click it and in the drop-down menu select Calculator Script > Multiply (float). Underneath the function drop-down there should now be a number (0). This is the multiplier. Set it to 3, for example.
Run the game. The Result Text should display 1 to begin with. When you click a button, it will multiply the value. You can press a button multiple times.
???
Profit
CalculatorScript.cs
using UnityEngine;
using UnityEngine.UI; // access to Text, Buttons, Canvas, etc
public class CalculatorScript : MonoBehaviour
{
public Text resultText; // Assign this in the inspector!
public float resultValue = 1; // use float for decimal points
// initialization
void Start()
{
// update the Text to show the default value.
DisplayResult();
}
// updates the displayed text
public void DisplayResult()
{
// if the Text field is assigned in the inspector
if (resultText != null)
{
// convert the double into a string and display as text
resultText.text = resultValue.ToString();
}
}
// CALCULATOR FUNCTIONS
// method function that can be called from other scripts or events
public void Multiply(float value)
{
// multiply the resultValue by this value.
resultValue *= value;
// same as: resultValue = resultValue * value;
// update the Text
DisplayResult();
}
// more example functions
// sets resultValue to 0
public void Clear()
{
resultValue = 0;
DisplayResult();
}
public void Divide(float value)
{
resultValue /= value;
DisplayResult();
}
public void Add(float value)
{
resultValue += value;
DisplayResult();
}
public void Subtract(float value)
{
resultValue -= value;
DisplayResult();
}
public void Power(float value)
{
resultValue = Mathf.Pow(resultValue, value);
DisplayResult();
}
public void Modulus(float value)
{
resultValue = resultValue % value;
DisplayResult();
}
// etc...
}
Answer by stba · Nov 13, 2016 at 09:25 PM
Thank you VERY much!!!! It worked like a charm. Great explenations! The only question that I have is when I multiply a big number it gives a number like so the one below (3.48E+07). What can I do to have a full number displayed?
[1]: /storage/temp/82125-capture.jpg
Due to rounding point error with floats, it won't do much good to show more zeros. But if you still want to...
The ToString() function in the line resultText.text = resultValue.ToString();
can be formatted with more places. For example, ToString("#########0.##########")
.
If you want greater precision than what a 'float' can provide, you could use a 'double' or another larger number type. This might be harder to use with events, though, since Unity only knows how to display a few parameter types like int, float, bool, string, etc, but not double. I.e. in the inspector you won't be able to choose a function with a parameter of type double.
Sublime gamer, thanks for all the help. I have the buttons assigned as multipliers and they are updating the "result text". I also have some buttons with a second multiplier updating a "result text 1". What I'm trying to do is to have a third text dividing "reslt text" by "result text 1". Since they are text I'm not sure how to go about the text calculating capabilities.
Thanks!
You could call the first CalculatorScript's functions with the argument set to the second CalculatorScript's resultValue, like in the script below. By calling the CalculatorScript's functions, we do not need to read the value from the text in the Text objects, and the other operator buttons will change the new result value after division, as you would expect.
Attach the below script to a 'Divide' button
Assign the numerator CalculatorScript as Calc 1, and the deno$$anonymous$$ator CalculatorScript as Calc 2
Set the button's OnClick event to call the DivideScript's 'Divide' function.
DivideScript.cs
using UnityEngine;
public class DivideScript : $$anonymous$$onoBehaviour
{
public CalculatorScript calc1; // numerator
public CalculatorScript calc2; // deno$$anonymous$$ator
// call calc1's Divide method with calc2's ResultValue
public void Divide()
{
calc1.Divide(calc2.resultValue);
}
// another example
public void $$anonymous$$ultiply()
{
calc1.$$anonymous$$ultiply(calc2.resultValue);
}
}
SublimeGamer,
Thanks again for the input. Very easy to use and VERY effective tool. I hope you don't $$anonymous$$d me asking two other questions.
First, I'm trying to add a second line of data to the deno$$anonymous$$ator. I was was able to add calc3, but I can't figure out how to script the calulation calc1+calc2, all of this divided by calc3. I guess this really shows my level of coding experience...
The second question that I have is that I have a main menu with different buttons opening different scenes. In those scenes, I have different drop down menus named location, size, height, etc to which we assigned the different multiplier as you suggested in my initial post.
What I'm trying to do is whenever I press a "save" button, located in all of the different scenes. This save button would save the text displayed from the top dropdown menu buttons (the dtopdown menu is set in a way that whenever I press any button from the list, the top button text changes to the the text of the button pressed), and also display the name of the scene from which the saved button was pressed into a different scene called "saved data" including. The save button would also save the text the result of the calculation called "result".
Thanks for taking the time to take a reading through this and again for all the help!
Best Regards!
If you have 3 CalculatorScripts, you can make a new script which first adds two of them, then divides by the third one, and then stores the result somewhere.
First add this function to CalculatorScript.cs
public void SetResultValue(float value) { resultValue = value; DisplayResult(); }
Create a new script for the calculation:
CalculationScript.cs
using UnityEngine; public class CalculationScript : $$anonymous$$onoBehaviour { public CalculatorScript a, b, c; public CalculatorScript result; public void DoCalc1() { // result = (a+b)/c result.SetResultValue( (a.resultValue + b.resultValue) / c.resultValue ); } }
Attach this script to your button. Assign three CalculatorScripts as a, b and c, as well as one for the result (which can be any CalculatorScript).
$$anonymous$$ake sure the button's OnClick event calls the CalculationScript's DoCalc1 function.
As for your second question, it seems like pretty advanced stuff for someone new to scripting to be trying to accomplish. Also, it is radically different to the calculator script discussed in this question. I would suggest having a go at it yourself, and if you run into trouble, make a new question specifically about that in Unity Answers.
Happy to help!
Your answer
Follow this Question
Related Questions
UI Text Editing Problem, What's Wrong? Need Anybody to Assist 1 Answer
Popup text in certain area when clicked 1 Answer
how can i make the name of a clicked button appears as text in another scene 1 Answer
Making text appear by pressing a button, only when player is close to the object 1 Answer
Button calls itself numerous times 0 Answers