Hi guys, i need some help from all of you, i’m really appreciate for your help guys.
i’m trying to make shop for my simple game, but i’m using hardcode and it’s really long code for me, i want to make the code look simple and great.
This is the screenshot for my shop and explanation what i want :
My shop :
This is my code example for page 1 :
function OnGUI () {
GUI.Label(Rect(Screen.width / 2 - 115, Screen.height / 2 - 60, 50, 50),"Cube");
GUI.Button(Rect(Screen.width / 2 - 115, Screen.height / 2 - 40, 50, 50),"IMG");
GUI.Label(Rect(Screen.width / 2 - 105, Screen.height / 2 + 10, 50, 50),"1 / 1");
GUI.Label(Rect(Screen.width / 2 - 55, Screen.height / 2 - 60, 50, 50),"Build tower");
GUI.Label(Rect(Screen.width / 2 - 55, Screen.height / 2 - 30, 50, 50),"P : 10");
GUI.Label(Rect(Screen.width / 2 - 55, Screen.height / 2 - 10, 50, 50),"S : 10");
GUI.Label(Rect(Screen.width / 2 - 55, Screen.height / 2 + 10, 50, 50),"R : 10");
if(GUI.Button(Rect(Screen.width / 2 - 100, Screen.height / 2 + 30, 50, 25),"Buy")){
isCube = true;
createBuilding = true;
}
GUI.Label(Rect(Screen.width / 2 + 15, Screen.height / 2 - 60, 50, 50),"Sphere");
GUI.Button(Rect(Screen.width / 2 + 15, Screen.height / 2 - 40, 50, 50),"IMG");
GUI.Label(Rect(Screen.width / 2 + 25, Screen.height / 2 + 10, 50, 50),"1 / 1");
GUI.Label(Rect(Screen.width / 2 + 75, Screen.height / 2 - 60, 50, 50),"Build tower");
GUI.Label(Rect(Screen.width / 2 + 75, Screen.height / 2 - 30, 50, 50),"P : 10");
GUI.Label(Rect(Screen.width / 2 + 75, Screen.height / 2 - 10, 50, 50),"S : 10");
GUI.Label(Rect(Screen.width / 2 + 75, Screen.height / 2 + 10, 50, 50),"R : 10");
GUI.Button(Rect(Screen.width / 2 + 30, Screen.height / 2 + 30, 50, 25),"Buy");
if(GUI.Button(Rect(Screen.width / 2 - 100, Screen.height / 2 + 30, 50, 25),"Buy")){
isSphere = true;
createBuilding = true;
}
}
at my shop there’s many page , so if user click >> user will face the second page, and then third page, and then fourth page and more page.
my problem :
- one page contain 2 building, so if i have 30 buildings, i need 15 pages for it and i need to code it manually like the code above very long code for 15 pages.
- my code above when i click buy button at Cube or Sphere, i manually change isCube or isSphere into true, but if i have 30 building, i need to code it manually for 30 buildings like this isCube = true, isSphere = true, isCylinder = true, isCapsule = true, and more
like this example code it’s only for 4 items, if i have 30 items then i must repeat this code for 30 times and not effective code.
if(GUI.Button(Rect(Screen.width / 2 - 100, Screen.height / 2 + 30, 50, 25),"Buy Cube")){
isCube = true;
createBuilding = true;
}
if(GUI.Button(Rect(Screen.width / 2 - 100, Screen.height / 2 + 30, 50, 25),"Buy Sphere")){
isSphere = true;
createBuilding = true;
}
if(GUI.Button(Rect(Screen.width / 2 - 100, Screen.height / 2 + 30, 50, 25),"Buy Cylinder")){
isCylinder = true;
createBuilding = true;
}
if(GUI.Button(Rect(Screen.width / 2 - 100, Screen.height / 2 + 30, 50, 25),"Buy Capsule")){
isCapsule = true;
createBuilding = true;
}
what i want :
- i need some simple code how to make pages.
- i need some simple code for buy button. so i know buy button for first page is to buy cube and sphere, second page for buy capsule and cylinder, and next
so i don’t need to code it so long for many items and many pages.
I hope you guys want to help me and can give me some example script with Javascript how to solved my problem.
sorry for my bad english.
Thanks ![]()