- Home /
Why does it give me an error in this small java script for sprint?
var curStam : float = 100.0;
var maxStam : float = 100.0;
var isSprinting : boolean = false;
var stamCooldown : boolean = false;
function OnGUI () {
GUI.Label(ResizeGUI(Rect(430,350,50,50)), "" + curStam);
if (GUI.Button(ResizeGUI(Rect(350,350,50,50)), "Run" && curStam > 0 && stamCooldown == false)) {
isSprinting = true;
}
else {
isSprinting = false;
}
if(isSprinting == true && curStam >= 0) {
curStam--;
}
if(isSprinting == false && curStam < 100.0) {
curStam++;
}
if(curStam == 0) {
stamCooldown = true;
}
if(curStam == 1.0) {
stamCooldown = false;
}
}
function ResizeGUI(_rect : Rect) : Rect
{
var FilScreenWidth = _rect.width / 800;
var rectWidth = FilScreenWidth * Screen.width;
var FilScreenHeight = _rect.height / 600;
var rectHeight = FilScreenHeight * Screen.height;
var rectX = (_rect.x / 800) * Screen.width;
var rectY = (_rect.y / 600) * Screen.height;
return Rect(rectX,rectY,rectWidth,rectHeight);
}
I'm trying to make a runescape style sprint bar, if you click on a button, it will activate sprinting so if you click somewhere, it sprints there, and when the curStam hits 0, you cant sprint anymore. Problem is the button, it worked with holding Shift in an update function, but I want it in a OnGUI.
Error is:
MissingMethodException: UnityEngine.GUI.Button
I tried to fix it by RepeatButton, does work tho. Any help greatly appreciated.
Answer by AeonIxion · Nov 18, 2013 at 07:31 PM
change
if (GUI.Button(ResizeGUI(Rect(350,350,50,50)), "Run" && curStam > 0 && stamCooldown == false))
to
if (GUI.Button(ResizeGUI(Rect(350,350,50,50)), "Run") && curStam > 0 && stamCooldown == false)
EDIT
if (GUI.Button(ResizeGUI(Rect(350,350,50,50)), "Run"))
returns true if you click on the button so every time you clicked on the button, isSprinting was set to true, but the next frame it was set back to false because you didn't keep clicking. With the following code, if you click on the button, isSprinting will be set to true if it was false and vice versa.
if (GUI.Button(ResizeGUI(Rect(350,350,50,50)), "Run")){
Debug.Log("clicked");
isSprinting = !isSprinting;
}
if(isSprinting == true && curStam > 0) {
Debug.Log("curstamdown" + curStam);
curStam--;
}
else if(curStam == 0)
{
isSprinting = false;
}
if(isSprinting == false && curStam < 100.0) {
Debug.Log("curstam up" + curStam);
curStam++;
}
Ah thanks that fixed the error, but even with RepeatButton, it will only make the curStam change by -1, not keep going down to 0. How can I fix that?
Thanks! Now it works perfectly. Would you know how to add something so it wont make it lower so fast? I tried InvokeRepeating("cur$$anonymous$$ax",1,1);
that didn't do anything. Any tips regarding that? Thanks again !
change curStam--
to curStam -= 0.1
. you can increase/decrease it as fast or slow as you want. You could change the rate depending on the player's level for example :)
Oh god another problem popped up. It starts at 100.01, and if I start it, and let it go down to 0, it goes to 0.0515 something. Why does it do that? Can I change that, without going back to int variable?