Input.GetButtonDown in OnTriggerEnter

Hey guys,

So I have this script for when the player walks into a trigger it gives him the option to upgrade his fire rate for his weapon. Here it is:

var fireRate : boolean;
var player : Transform;
var playerMoney : int;
var upgradeCost : int;
var fireDisp : boolean;
var dispX : int;
var dispY : int;
var dispX2 : int;
var dispY2 : int;
var style : GUIStyle;


function Start()
{
	 style.normal.textColor = Color.black;
}

function Update()
{
	playerMoney = player.GetComponent(PlayerAttributes).Money;
	
	if(fireRate)
	{
		upgradeCost = 10;
	}

}


function OnTriggerStay(other : Collider){
	if(other.tag == "Player")
	{
		print("Entered");
		if(fireRate)
		{
		print("Fire Rate Upgrade Station");
		fireDisp = true;
    		if(playerMoney > upgradeCost)
    		{
    			if(Input.GetButtonDown("F")){
    			print("Updated!");
    			//Upgrade Firerate here
    			playerMoney -= upgradeCost;
    			
    		}
    	}
	}
}
}

function OnTriggerExit(other : Collider)
{
	if(other.tag == "Player")
	{
		fireDisp = false;
	}
}
function OnGUI()
{
	if(fireDisp == true)
	{
	 GUI.Label (Rect (dispX, dispY, 100, 20), "Cost: 10",style);
    GUI.Label (Rect (dispX2, dispY2, 100, 20), "You have:"+playerMoney,style);
    }
}

The only problem here is this section:

    		if(playerMoney > upgradeCost)
    		{
    			if(Input.GetButtonDown("F")){
    			print("Updated!");
    			//Upgrade Firerate here
    			playerMoney -= upgradeCost;
    			
    		}

I can’t get it to print “Updated” or anything when I hit F. I get no errors or anything, just nothing happens. I dont see anything wrong with the script. Whats up?

I believe the issue is that onEnter is called for one frame. It would be virtually impossible for your user to match this frame with pressing the key. Use onTriggerStay.

This might be awesome for tournament fighting game players who can hit a button with 1/60th of a second precision, but for the rest of us you’re asking too much. OnTriggerEnter only happens once, and GetButtonDown checks for a down event which happens for one frame.

You want to use OnTriggerStay, which fires once per frame while the triggers remain touching. That way you can take all the time you need.

I’m with renman3000, although I’m also wondering if it’s case sensitive.

Ahh ok, I will check that and be right back with the results

Ok so now it prints but it wont subtract anything.

As a bonus, if you only want the key to be used once per enter, stay, use a bool if true, key can be used. Use key, set to false. Allow it to switch back to true on exit or enter.

try a

Debug.Log(PlayerMoney);

before the calculation is calculated to make sure player money actually has a value…

Also I know everyone has their own style but its usually convention to use camelCase for variables.

as I am working in the editor I am looking both at the amount of money the player has (which starts out as fifty) as well as what the upgrade station THINKS it has, which is also 50. On top of that if you look at the code I have it set to display the value of PlayerMoney.

Also camelCase?

Just cause the values in the inspector are what they should be doesn’t mean they are…

This line:

PlayerMoney = Player.GetComponent(PlayerAttributes).Money;

I don’t think that is actually setting the amount of money to what you want it to be… That is the reason I told you to check

Also wherever Money is in the PlayerAttributes script how are you setting the value? in update? by default when you declare the variable? Its hard to tell what is going on without actually know how the value is being set in the first place.

camelCase = playerMoney not PlayerMoney

Im not sure I understand you. If it is a value in the inspector than that is its value… It updates in realtime, right? The script above works fine because when I’m playing and I change the PlayerMoney variable in the inspector it reflects the changes on the GUI label and in the inspector of the upgrade station.

also I updated with camelCase. Thanks for letting me know about that.

What I’m trying to say is your performing a very basic calculation and for some reason its not updating correct? I’m trying to help you find out why…

Usually initialization settings like this line are placed in the Start () method since it doesn’t need to be refreshed every update:

playerMoney = player.GetComponent(PlayerAttributes).Money;

Also, I can’t really remember exactly, but I’m not sure if that line would actually work as a pointer or if you should link to the PlayerAttributes component and then make the adjustment by calling playerAttributes.money -= upgradeCost instead. I’m probably too tired to be giving advice right now, but try that and see if it helps.

Yes Moving that line to start should fix the problem. I thought it was already in start… :frowning:

Thanks a ton! I didn’t even think about it. It worked perfectly.