Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Vehicle purchase system

Discussion in 'Game Design' started by varunvp, Sep 26, 2016.

  1. varunvp

    varunvp

    Joined:
    Jul 11, 2014
    Posts:
    57
    Hey forum,
    I know this topic might seem trivial to some of you, but since I am implementing this for the first time, I want the knowledge of this system. Badly!
    It's going to be a simple one. One of my resources folder has the prefabs of all the vehicles. The selection screen displays them fine too. I also have a static 'credit' variable, which is stored on the player profile.
    Now, the question is, how do I store the details related to each vehicle? It's cost, whether it's locked, whether it's bought, it's statistics, and stuff. The easiest way seems to be having some type of script on the prefabs themselves, with variables for each statistic. Any other better solutions?
    Thanks!
     
  2. ZakCollins

    ZakCollins

    Joined:
    Jun 11, 2014
    Posts:
    52
    Your solution sounds correct to me. If you want to make it flexible so that in the future you can purchase other things besides vehicles, I would create a PurchaseableItem class that has cost, locked status, and description. Then you could have a Vehicle class that inherits from PurchaseableItem and also has statistics about the vehicle like its speed and such. You should look into polymorphism if you don't understand the inherit stuff. It is very powerful.
     
  3. varunvp

    varunvp

    Joined:
    Jul 11, 2014
    Posts:
    57
    I totally understand inheriting, and I think this will be the way I'll be going. Another doubt, I can keep the variables as normal public stuff, right? I mean, does there have to be any other modifier to the variables, since they will be storing important vehicle statistics? For example, [SerializeField] or anything?
    Thank you for the reply!:)
     
  4. ZakCollins

    ZakCollins

    Joined:
    Jun 11, 2014
    Posts:
    52
    You can keep them as public if you want. I think the best practice would be to make them private or protected, and then have getters and setters for them. The reason for this is that it makes things easier to debug (and you can control how the variable is being changed). If you are manipulating these values through scripts then it will be much easier to keep track of where the variable is being changed if you have getters and setters. I've run into quite a few situations where a variable was being changed to something incorrect and had to track down where it is being changed. If you have a setter, all you have to do is put a break point in there. If the variable is public and you are changing it directly in a lot of different places, you have to set a break point at every single place you might be changing it. If you want to edit your private or protected variable in the editor (which you probably will), then you will need [SerializeField]. Public variables are fine though if it isn't going to be a big project with multiple people working on it.
     
  5. varunvp

    varunvp

    Joined:
    Jul 11, 2014
    Posts:
    57
    I'll go with good ol' public then. Thank you!
     
  6. varunvp

    varunvp

    Joined:
    Jul 11, 2014
    Posts:
    57
    I was just about to implement the solution, while I was struck with another doubt. When I change the variables in the purchasable item script on the prefabs, will the changes be retained? Like if I make bool isBought to true, will it be retained when I close and open the game?