Inside a script, I have an int called food, and I want it to decrease by one when I press F. It starts at 5. Problem is, I just couldn’t make it work. I don’t know how to change int values from inside the code. What would be the simplest script solution?
void Update()
{
if (Input.GetKeyDown(KeyCode.F))
{
food–;
}
}
Create a c# script called Player, paste this in, save, attach to some gameobject in scene and play.
using com.events;
using UnityEngine;
public class Player:MonoBehaviour {
private int _food = 12;
private void Update() {
if(Input.GetKeyDown(KeyCode.F)) {
Eat();
}
}
private void Eat() {
if(_food >= 5) {
_food -= 5;
Debug.Log("nom nom");
}
else{
Debug.Log("not enough food:"+_food);
}
}
}