K
K
kaf_ca2021-03-11 18:03:23
Unity
kaf_ca, 2021-03-11 18:03:23

How to set variables via InputField in Unity and count values?

Hello, I want to make an application for calculating damage using formulas.

I make an InputField in which the user must enter a damage value, for example 100
Then I make a second InputField in which you need to enter a protection parameter.
Then I need to display the result on the screen where Damage - Protection = Amount of damage dealt.

How to do it? I need to set values ​​to variables through InputField, then calculate all this using the damage calculation formula and display the result in text by pressing the button.

Unity swears that in the first place, the variables set through the InputField type are String, but int is needed.
Secondly, it swears "cs0236 c field initializer cannot access a non-static field method or properties"
What scripts and where to throw them? If I need to add a "penetration" parameter that will reduce defense in %?
I'm a beginner and I can't figure out how to make such a simple functionality... Please help me.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
oleg_ods, 2021-03-11
@oleg_ods

Values ​​from InputField comes in the form of a string (string) and before doing any calculations you need to convert the values. The int type has a Parse method for this .
Regarding cs0236, read the documentation:
Tyts
Most likely you are trying to do something similar.
PS And it's best to learn at least the basics of the C # language before climbing into Unity. Then there will be fewer questions like this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question