Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Help - Scripting buy item at shop

Discussion in 'Scripting' started by quantum_rez, Nov 17, 2012.

  1. quantum_rez

    quantum_rez

    Joined:
    Oct 23, 2012
    Posts:
    35
    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 :
    Code (csharp):
    1. function OnGUI () {
    2. GUI.Label(Rect(Screen.width / 2 - 115, Screen.height / 2 - 60, 50, 50),"Cube");
    3.     GUI.Button(Rect(Screen.width / 2 - 115, Screen.height / 2 - 40, 50, 50),"IMG");
    4.     GUI.Label(Rect(Screen.width / 2 - 105, Screen.height / 2 + 10, 50, 50),"1 / 1");
    5.     GUI.Label(Rect(Screen.width / 2 - 55, Screen.height / 2 - 60, 50, 50),"Build tower");
    6.     GUI.Label(Rect(Screen.width / 2 - 55, Screen.height / 2 - 30, 50, 50),"P : 10");
    7.     GUI.Label(Rect(Screen.width / 2 - 55, Screen.height / 2 - 10, 50, 50),"S : 10");
    8.     GUI.Label(Rect(Screen.width / 2 - 55, Screen.height / 2 + 10, 50, 50),"R : 10");
    9.     if(GUI.Button(Rect(Screen.width / 2 - 100, Screen.height / 2 + 30, 50, 25),"Buy")){
    10.         isCube = true;
    11.         createBuilding = true;
    12.     }
    13.    
    14.     GUI.Label(Rect(Screen.width / 2 + 15, Screen.height / 2 - 60, 50, 50),"Sphere");
    15.     GUI.Button(Rect(Screen.width / 2 + 15, Screen.height / 2 - 40, 50, 50),"IMG");
    16.     GUI.Label(Rect(Screen.width / 2 + 25, Screen.height / 2 + 10, 50, 50),"1 / 1");
    17.     GUI.Label(Rect(Screen.width / 2 + 75, Screen.height / 2 - 60, 50, 50),"Build tower");
    18.     GUI.Label(Rect(Screen.width / 2 + 75, Screen.height / 2 - 30, 50, 50),"P : 10");
    19.     GUI.Label(Rect(Screen.width / 2 + 75, Screen.height / 2 - 10, 50, 50),"S : 10");
    20.     GUI.Label(Rect(Screen.width / 2 + 75, Screen.height / 2 + 10, 50, 50),"R : 10");
    21.     GUI.Button(Rect(Screen.width / 2 + 30, Screen.height / 2 + 30, 50, 25),"Buy");
    22.     if(GUI.Button(Rect(Screen.width / 2 - 100, Screen.height / 2 + 30, 50, 25),"Buy")){
    23.         isSphere = true;
    24.         createBuilding = true;
    25.     }
    26. }
    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 :
    1. 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.
    2. 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.
    Code (csharp):
    1. if(GUI.Button(Rect(Screen.width / 2 - 100, Screen.height / 2 + 30, 50, 25),"Buy Cube")){
    2.         isCube = true;
    3.         createBuilding = true;
    4.     }
    5.  
    6. if(GUI.Button(Rect(Screen.width / 2 - 100, Screen.height / 2 + 30, 50, 25),"Buy Sphere")){
    7.         isSphere = true;
    8.         createBuilding = true;
    9.     }
    10.  
    11. if(GUI.Button(Rect(Screen.width / 2 - 100, Screen.height / 2 + 30, 50, 25),"Buy Cylinder")){
    12.         isCylinder = true;
    13.         createBuilding = true;
    14.     }
    15.  
    16. if(GUI.Button(Rect(Screen.width / 2 - 100, Screen.height / 2 + 30, 50, 25),"Buy Capsule")){
    17.         isCapsule = true;
    18.         createBuilding = true;
    19.     }

    what i want :
    1. i need some simple code how to make pages.
    2. 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 :D
     
    Last edited: Nov 17, 2012
  2. quantum_rez

    quantum_rez

    Joined:
    Oct 23, 2012
    Posts:
    35
    please, anyone can help me.
    i'm trying many way to find it out, but until now i can't solved my problem
     
  3. Bunzaga

    Bunzaga

    Joined:
    Jan 9, 2009
    Posts:
    202
    I would look into GUI Content or GUI styles, as well as not hard coding your information like that.

    You basically want ONE structure, and think of how to populate it with all the different data.

    You could use an array of store objects, which contain the individual strings, textures, etc. Then as you click back and next, you change the current Index, and populate the data pulled from your game objects.

    The way your buy button should work, is when they click it, you see what is the index is loaded into that spot, you shouldn't have to iterate like that.

    Examples...
    Code (csharp):
    1.  
    2. if(GUI.Button(Rect(Screen.width / 2 - 100, Screen.height / 2 + 30, 50, 25),items[currentIndex].name)){
    3.   BuyObject(currentIndex);
    4. }
    5.  
    6. GUI.Label(Rect(Screen.width / 2 - 115, Screen.height / 2 - 60, 50, 50),items[currentIndex].name);
    7. GUI.Button(Rect(Screen.width / 2 - 115, Screen.height / 2 - 40, 50, 50),items[currentIndex].img);
    8. GUI.Label(Rect(Screen.width / 2 - 105, Screen.height / 2 + 10, 50, 50),items[currentIndex].min+"/"+items[currentIndex].max);
    9.  GUI.Label(Rect(Screen.width / 2 - 55, Screen.height / 2 - 60, 50, 50),items[currentIndex].description);
    10.  
    11.  
     
    Last edited: Nov 19, 2012
  4. quantum_rez

    quantum_rez

    Joined:
    Oct 23, 2012
    Posts:
    35
    thanks Bunzaga for your reply, i get it now. But i don't know how can we use array to strore object which contain name, img, description and etc?

    I know the array would like this :

    Code (csharp):
    1. var items : String[] = ["items1", "items2","items3"];
    how can the array contain name, img, description etc? can you please give me the example code?

    Thanks