For loop work well except on third state
Hello I am not a developper but i am doing my best and sorry for my "google helped English ! =) Thank you for your help , this problem is inexplainable for me !
I have a button that increases the size of the gauge but the "for" loop does not work only on the 3rd state. (I focus only on the widht of jauge problem) The loop has 6 states If I execute the function in question " update () " it works but it drops the frame per second . The function Concerned by the problem is: setSizeBarre ();
please help me undestand the problem, I think it is a order of execution problem or a runtime problem but I dont know... here is a picture (the jauge is incremented in width) using UnityEngine; using System.Collections; using UnityEngine.UI;
public class jauge : MonoBehaviour
{
public static int jaugeCase;
public string typeEvol;
enum listTypeEvol { Engine, Armor, Weapon };
listTypeEvol listTypeEvols;
private GameObject laBarre;
private bool limit = true;
private int limitint;
private int priceEvol;
private GameObject priceEvolTxt;
public globals globals;
public void calculatePrice()
{
switch (typeEvol)
{
case ("Engine"):
priceEvolTxt = GameObject.FindGameObjectWithTag("textEngine").gameObject;
priceEvolTxt.name = "Tester";
priceEvolTxt.GetComponent<Text>().text = priceEvol.ToString();
limitint = PlayerPrefs.GetInt("_saveEngine");
break;
case ("Weapon"):
priceEvolTxt = GameObject.FindGameObjectWithTag("textWeapon").gameObject;
priceEvolTxt.name = "TesterWeapon";
priceEvolTxt.GetComponent<Text>().text = priceEvol.ToString();
limitint = PlayerPrefs.GetInt("_saveWeapon");
break;
case ("Armor"):
limitint = PlayerPrefs.GetInt("_saveArmor");
break;
default:
print("par defautl");
break;
}
switch (limitint)
{
case 0:
priceEvol = 25;
print("prix de leveol " + priceEvol);
break;
case 1:
priceEvol = 25;
print("prix de leveol " + priceEvol);
break;
case 2:
priceEvol = 50;
print("prix de leveol " + priceEvol);
break;
case 3:
priceEvol = 75;
print("prix de leveol " + priceEvol);
break;
case 4:
priceEvol = 100;
print("prix de leveol " + priceEvol);
break;
case 5:
priceEvol = 150;
print("prix de leveol " + priceEvol);
break;
case 6:
priceEvol = 200;
print("prix de leveol " + priceEvol);
break;
default:
print("par defautl");
break;
}
}
public void testenum()
{
while (limit)
{
limitint++;
laBarre = this.gameObject;
laBarre.transform.GetChild(1).name = "nouveua";
calculatePrice();
if (globals.gems >= priceEvol)
{
laBarre.transform.GetChild(0).GetComponent<RectTransform>().sizeDelta += new Vector2(37.5f, 0);
updateShip();
return;
}
else {
print("Pas assez d'argent");
return;
}
}
return;
}
public void updateShip()
{
int tempLevel;
switch (typeEvol)
{
case "Engine":
print("Engine upgraded !");
tempLevel = PlayerPrefs.GetInt("_saveEngine");
print(PlayerPrefs.GetInt("_saveEngine"));
if (tempLevel == 6 || limitint == 6)
{
limit = false;
print("Deja fini d'upgrader");
return;
}
else if (tempLevel < 6)
{
tempLevel++;
setSizeBarre();
globals.refreshData(arrstring: "_saveEngine", arr: tempLevel);
print(PlayerPrefs.GetInt("_saveEngine"));
}
break;
case "Weapon":
print("Engine upgraded !");
tempLevel = PlayerPrefs.GetInt("_saveWeapon");
print(PlayerPrefs.GetInt("_saveWeapon"));
if (tempLevel == 6 || limitint == 6)
{
limit = false;
print("Deja fini d'upgrader");
return;
}
else if (tempLevel < 6)
{
tempLevel++;
setSizeBarre();
globals.refreshData(arrstring: "_saveWeapon", arr: tempLevel);
print(PlayerPrefs.GetInt("_saveWeapon"));
}
break;
default:
print("Rien de caucher");
break;
}
return;
}
void Update()
{
****//IF I DO THIS IT IS WORK FINE BUT THE FPS FALL TO 10FPS :/
// setSizeBarre();****
}
void Start()
{
setSizeBarre();
}
void setSizeBarre()
{
switch (typeEvol)
{
case ("Engine"):
print(limitint + "est mon niveau pour engine");
laBarre = this.gameObject;
limitint = PlayerPrefs.GetInt("_saveEngine");
break;
case ("Weapon"):
laBarre = this.gameObject;
limitint = PlayerPrefs.GetInt("_saveWeapon");
break;
case ("Armor"):
laBarre = this.gameObject;
limitint = PlayerPrefs.GetInt("_saveArmor");
break;
default:
print("par defautl");
break;
}
for (int i =0; i < limitint; i++)
{
print ( limitint + "est la limite de la boucle");
laBarre.transform.GetChild(0).GetComponent<RectTransform>().sizeDelta = new Vector2(37.5f * limitint, 75.2f) ;
}
}
void OnGUI()
{
calculatePrice();
if (limitint == 6)
{
limit = false;
}
}
public void resetData()
{
PlayerPrefs.DeleteAll();
}
}
Answer by DiegoSLTS · Sep 20, 2016 at 09:07 PM
I don't understand what those 3 and 6 states mean, but for the framerate drop try to remove those print calls in the update and anything that prints to the console. It has a big impact in performance if you're printing several times on every frame.
In fact the problem os "states" was occured a the third time the button was pressed. thank you for the tips of print i have too much print/frame. Finally I solved the problem putting the setSizeBarre (); partially on update() (only the size change)
Your answer
![](https://koobas.hobune.stream/wayback/20220612093139im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do i make Unity seamlessly loop my background music? 5 Answers
How do I get an animation to loop back from a certain point? 1 Answer
How should I prep audio assets for looping so as to avoid pops clicks etc. 0 Answers
Trigger a certain number of loop in animation on button down 0 Answers
How to make object fall loop? 0 Answers