Im attempting to add and minus from a text value using buttons. What my script currently does is - when I +++++ it shows 5, then I use the - button 3 times and it gives me -3, I then use + and I get 6. I hope this makes it clear.
public class PlusMinusScript : MonoBehaviour {
private int textNumber;
public Text TextObject = null;
public void addOne(){
if (TextObject != null) {
textNumber++;
TextObject.text = textNumber.ToString ();
}
}
public void minusOne(){
if (TextObject != null) {
--textNumber;
TextObject.text = textNumber.ToString();
}
}
}
@hellium Inside my plus minus script I should put two buttons? @TreyH Same question to you Thanks for your speedy replies. Im hoping i can get this now
As expected, you have attached the PlusMinusScript
script to two scripts. Since the value of textNumber
is not shared between these two scripts, your problem occurs.
- Attach the
PlusMinusScript
to only one gameObject (to your TextObject for example)
- Drag & Drop the gameObject holding the
Text
component to the public field of the PlusMinusScript
- Select your first button, drag & drop the gameObject in step #1, and select the
addOne
function
- Select your second button, drag & drop the gameObject in step #1, and select the
minusOne
function
YAYAYAYYY!!! Thank you so much for your help and patience @TreyH