- Home /
Optimizing Code
Hi I have some code for my game that checks what level you are on and if you are a certain level it enables the interactable component for the buttons one by one each level. I am new to programming so i was wondering if someone could help me optimize my code as i have to individually check each level in the update function at the moment Here is my code
public class LevelManager : MonoBehaviour {
public Click click;
public GameObject levelholder;
public float bargain;
public float researchpoints;
public float researchskill;
public float researchlevel;
public UpgradeManager ugpr;
public AudioSource audio1963;
public Button research;
public UnityEngine.UI.Text Rlevel;
public float rcost;
public float baseCost;
public UnityEngine.UI.Text Rcost;
public void Update()
{
Rlevel.text = "Research Level: " + researchlevel;
Rcost.text = "Cost to research: " + rcost;
PlayerPrefs.SetFloat("ResearchL", researchlevel);
PlayerPrefs.SetFloat("Rcost2", rcost);
if (researchlevel < 3f)
{
ugpr.item3.interactable = false;
ugpr.item4.interactable = false;
ugpr.item5.interactable = false;
ugpr.item6.interactable = false;
ugpr.item7.interactable = false;
ugpr.item8.interactable = false;
ugpr.item9.interactable = false;
ugpr.item10.interactable = false;
}
if(researchlevel == 4f)
{
ugpr.item3.interactable = true;
ugpr.item4.interactable = false;
ugpr.item5.interactable = false;
ugpr.item6.interactable = false;
ugpr.item7.interactable = false;
ugpr.item8.interactable = false;
ugpr.item9.interactable = false;
ugpr.item10.interactable = false;
}
if (researchlevel == 5f)
{
ugpr.item3.interactable = true;
ugpr.item4.interactable = true;
ugpr.item5.interactable = false;
ugpr.item6.interactable = false;
ugpr.item7.interactable = false;
ugpr.item8.interactable = false;
ugpr.item9.interactable = false;
ugpr.item10.interactable = false;
}
if (researchlevel == 6f)
{
ugpr.item3.interactable = true;
ugpr.item4.interactable = true;
ugpr.item5.interactable = true;
ugpr.item6.interactable = false;
ugpr.item7.interactable = false;
ugpr.item8.interactable = false;
ugpr.item9.interactable = false;
ugpr.item10.interactable = false;
}
if (researchlevel == 7f)
{
ugpr.item3.interactable = true;
ugpr.item4.interactable = true;
ugpr.item5.interactable = true;
ugpr.item6.interactable = true;
ugpr.item7.interactable = false;
ugpr.item8.interactable = false;
ugpr.item9.interactable = false;
ugpr.item10.interactable = false;
}
if (researchlevel == 8f)
{
ugpr.item3.interactable = true;
ugpr.item4.interactable = true;
ugpr.item5.interactable = true;
ugpr.item6.interactable = true;
ugpr.item7.interactable = true;
ugpr.item8.interactable = false;
ugpr.item9.interactable = false;
ugpr.item10.interactable = false;
}
if (researchlevel == 9f)
{
ugpr.item3.interactable = true;
ugpr.item4.interactable = true;
ugpr.item5.interactable = true;
ugpr.item6.interactable = true;
ugpr.item7.interactable = true;
ugpr.item8.interactable = true;
ugpr.item9.interactable = false;
ugpr.item10.interactable = false;
if (researchlevel == 10f)
{
ugpr.item3.interactable = true;
ugpr.item4.interactable = true;
ugpr.item5.interactable = true;
ugpr.item6.interactable = true;
ugpr.item7.interactable = true;
ugpr.item8.interactable = true;
ugpr.item9.interactable = true;
ugpr.item10.interactable = false;
if (researchlevel > 10f)
{
ugpr.item3.interactable = true;
ugpr.item4.interactable = true;
ugpr.item5.interactable = true;
ugpr.item6.interactable = true;
ugpr.item7.interactable = true;
ugpr.item8.interactable = true;
ugpr.item9.interactable = true;
ugpr.item10.interactable = true;
}
}
}
So i was wondering is there a better way to disable all buttons except for 3 when on level 4 and all buttons except for 3 and 4 when on level 5 and so on without a huge block of long code
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Vuforia Target Image scene not appearing in game 0 Answers
Import Unity scene inside of Visual Studio Windows form 0 Answers
2D game Unity5 1 Answer