- Home /
Part of a function called from OnGUI doesn't work C#
The second part of my function 'DisplayUnits' that i call from OnGUI sometimes doesn't run (/get "played"). This is my code:
void OnGUI () {
GUI.color = bla;
if(myGameData.isInGame){
DisplayUnits (null, scaledRect.sSquare (10, 10, 40), true);
DisplayUnits (myGameData.charData.weapons[myGameData.charData.CurrentWeapons[0]],
scaledRect.sSquare (15, 145, 40), false);
DisplayUnits (myGameData.charData.weapons[myGameData.charData.CurrentWeapons[1]],
scaledRect.sSquare (245, 145, 40), false);
}
}
private void DisplayUnits (Weapon w, Rect pos, bool disLife){
Texture icon;
float amount;
if(disLife){
amount = myGameData.lives;
icon = lifeTexture;
}
else{
if(w.level == 0)
return;
amount = w.currentAmmo;
icon = w.icon;
}
if(amount < 1)
return;
Rect iconRect = new Rect(pos.center.x - pos.width / 3.2f, pos.center.y - pos.height / 3.2f, pos.width / 1.6f, pos.height / 1.6f);
GUI.DrawTexture (pos, middle);
GUI.DrawTexture (iconRect, icon);
Matrix4x4 original = GUI.matrix;
for(float layer = 0; layer < amount / 3; layer++){
for(int cnt = 0; cnt < 3; cnt++){
if(layer * 3 + cnt + 1 > amount)
return;
float angle = myGameData.gametime * 20 + cnt * 120 + layer * 30 + myGameData.gametime * Mathf.Pow (-layer, layer) * 10;
float scale = 1.1f + (layer * .2f);
GUIUtility.RotateAroundPivot (angle, pos.center);
GUIUtility.ScaleAroundPivot(new Vector2(scale, scale), pos.center);
GUI.DrawTexture (pos, border);
GUI.matrix = original;
}
}
//Cooldown
if(!disLife){
if(debug){
Debug.Log (myGameData.gametime);
Debug.Log (w.coolDown[w.level]);
Debug.Log (w.lastUsed);
debug = false;
}
if(myGameData.gametime - w.lastUsed > w.coolDown[w.level] || w.lastUsed == 0){
}
else{
GUIStyle gStyle = new GUIStyle();
gStyle.fontSize = 60 * Screen.width / 900;
GUI.Label (iconRect, ((int)(w.coolDown[w.level] - (myGameData.gametime - w.lastUsed))).ToString (), gStyle);
}
}
}
The part that doesn't seem to get played is after the comment //Cooldown. To find what the problem was, I exposed a public bool 'debug' in the editor. When I turned this bool on, the debug statement doesn't do anything, leading me to the conclusion that the second part doesn't get played, however the gui elements in the first part of the function: DisplayUnits, get displayed and keep spinning, so that part works.
I don't seem to be able to find what I did wrong, so I would appreciate any help.
*The return statements in the begin of the function aren't the cause of the problem since the gui textures in the first part are still showing.
Answer by Spinnernicholas · Dec 30, 2013 at 07:46 PM
It might also be:
if(layer * 3 + cnt + 1 > amount)
return;
I think you want continue:
if(layer * 3 + cnt + 1 > amount)
continue;
[OLD] This code:
if(amount < 1)
return;
causes the method to return sometimes. Which prevents anything else in the method from running after it.
if that was the cause of the problem, i wouldn't be able to see the gui elements that i used after the return statement, so that can't be the problem :/
Can you pinpoint where it cuts off and what do you mean by doesn't get "played". I thought that meant the same as doesn't run, which would prevent some of the gui from appearing. Are you saying that the Gui is displaying 100% correctly, but isn't responding to interaction?
The GUI before the comment: //cooldown runs correctly, but after that, it doesn't seem to work, because the gui and debug statements after that don't get displayed, and the public bool 'debug' doesn't get changed to false
Your answer
Follow this Question
Related Questions
GUILayout not working for me C# 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Can Unity track the changes I made to my DLL? 0 Answers
What's wrong with my code? C# 1 Answer