- Home /
Decrease variable after pressing a button
I was wondering how I would go about setting up a variable in my code that whenever I interacted with a certain game object the number for that variable would go down. So far, I have code that allows the player to interact with the object in the first place after pressing a button.
void Update() {
if (Input.GetButtonDown ("Interact") && currentInterObj) {
if (currentInterObjScript.npc) {
currentInterObj.SendMessage ("TriggerDialogue");
}
if (currentInterObjScript.interactable) {
currentInterObj.SendMessage ("GoToClass");
}
if (currentInterObjScript.inventory) {
inventory.AddItem (currentInterObj);
currentInterObj.SendMessage ("DoInteraction");
}
}
if (Input.GetButtonDown ("Continue") && currentInterObj) {
if (currentInterObjScript.npc) {
currentInterObj.SendMessage ("ContinueDialogue");
}
}
}
Answer by Ambrose998800 · Apr 01, 2018 at 08:52 PM
int VariableToGoDown = 100;
void Update() {
if (Input.GetButtonDown ("Interact") && currentInterObj) {
VariableToGoDown--;
//Do whatever you want to do else...
}
if (Input.GetButtonDown ("Continue") && currentInterObj) {
if (currentInterObjScript.npc) {
currentInterObj.SendMessage ("ContinueDialogue");
}
}
}
Your answer
Follow this Question
Related Questions
Help needed on c# code which all of you will find basic apart from me 1 Answer
Attaching a "Mass" variable to all or most gameObjects and how to access them 1 Answer
cannot drag script to player.Guitext error,cannot drag player script to the player in hierarchy 2 Answers
Script that stores gameobject hit by raycast keeps getting a NullReferenceException error. 2 Answers
Scripts accessing one another (JS) 0 Answers