- Home /
Same Input?
Hello, I want to make invetory for my game. I need with key " I " to open and close my invetory. I tried with boolean like: First I made a variable (a : int = 0) and set it to 0. And I said if a = 0 then do the input stuff(i) and set the variable a to 1. Then I said if a = 1 then do the input stuff (i) with the same key. It doesn't work. Any suggestions?
Thanks in advance :)
$$anonymous$$gestion 1: Don't describe code, paste the snippet.
$$anonymous$$gestion 2: If you have 2 states, don't use integer, use boolean or if you want your code more descriptive, use enumeration.
$$anonymous$$gestion 3: Always use input configuration,never bind directly on key. This way you can change key as you want.
Here is my script:
//inventory
static var inventoryArray : int[] = [1,2,0,0,0];
var inventoryText : GameObject;
var audio1: AudioSource;
var en = false;
var t : boolean = true;
var a : int = 0;
function Start(){
var aSources = GetComponents(AudioSource);
audio1 = aSources[1];
}
//inventoryArray[0]++;
function Update(){
if(inventoryArray[0] == 0){
inventoryText.guiText.text = "No heal potions available";
}
}
function OnGUI(){
if(Input.Get$$anonymous$$eyDown("i")){
en = true;
Screen.showCursor = true;
}
if(en==true){
GUI.Label(Rect(10, 10, 100, 200), "Heal Potion " + inventoryArray[0]);
if(GUI.Button(Rect(90, 12.2, 40, 17), "Heal")){
if(inventoryArray[0] > 0){
PlayerHealth.curHealth += 100;
inventoryArray[0] -= 1;
en = false;
Screen.showCursor = false;
}
}
}
if(en==false){
inventoryText.guiText.text = "";
}
}
add this if statement i believe
function OnGUI(){
if(Input.Get$$anonymous$$eyUp("i")){
en = false;
Screen.showCursor = false;
OnGUI() gets called multiple time per frame, so you want to move your Get$$anonymous$$ey*() code to Update(). Not sure of your question, but @Aug749 points out a fix for a problem.
Your answer
Follow this Question
Related Questions
My script for a "shield" function goes on/off constantly when it's supposed to be on 1 Answer
Having trouble checking if the player is in a trigger. 2 Answers
I am trying to create a leaning mechanic for an FPS game but I have one problem 1 Answer
Script error? BCE0077 0 Answers
Boolean and input relation 0 Answers