Best method to keep track and instantiate vehicle prefabs?

Hi folks

I hope someone from the community will give me their 2 cents. I have been looking at a lot of answers from the community as well as resources here and there.

I want to build a simple racing game. I have already built some racing mechanics and menus etc… My mental block is where vehicle selection / purchase comes in. I want to keep it as dynamic as possible, but basically I want a dealership type scene, where I can have a few vehicles, the player can purchase etc, have the vehicle in the garage scene and take it racing / driving etc.

My predicament is - What would be the best way to work with these vehicle prefabs? Is it beneficial to instantiate all of them in a class that persists throughout the game, or using the Resources.Load (which according to most forums is not the best practice) or Instantiating them as required? I am hesitant going the Instantiate route because in the situation of a car dealership where I will spawn a random set of vehicles from all vehicles based on price etc, the prefabs are not known (unless I define all these rules in my preload scene). I don’t know how efficient it would be to add an array of all vehicles in every scene and nitpick from there, or if there is a more dynamic and efficient way to do this.

I am basically hoping someone might be able to point me in the right direction on how to handle dealership cars and handling player owned vehicles when purchased. I am fairly proficient in C#… I think I am just in a bit of mental block at the moment.

Thanks everyone

Could go for the instantiate route but load a very lightweight version of the prefab, does it have to be the 3D model? If not you could load an icon or texture which could be a saved render of the vehicle, could have multiple images per texture and then load the full thing when they want to inspect it visually?

Because I don’t know the exact design of your game, I’ll provide some pointers that will hopefully guide you.

How you accomplish this task really depends on the size of each car asset, how many cars you plan to have in the game, target platform, and performance goals.

Keep in mind that each car you instantiate will take up space in memory. On a PC, this may be negligible but can become significant on a mobile device. Depending on the design of the game and memory constraints, you may:

a) choose to instantiate each car as needed - depending on the size of the asset, may produce none or a slight noticeable drop in frame rate, lowest memory overhead,

b) a couple of cars at a time - most likely intermittent loads as groups of cars become instantiated, medium memory overhead, or

c) pool the entire array of cars at load time that could persist through the entire game or be re-instantiated at the beginning of a level - longer load time at the beginning of the game / level, but quick showing of cars as you pull them from the array throughout the game, largest memory overhead

When it comes to maintaining a selection of cars, there are too many approaches and combination of methods for me to list, but here are a couple strategies that you might consider:

a) Keep a list of all your cars and their stats in a .json or .xml file. This requires more upfront work, but is one of the best ways to maintain a large amount of cars,

b) Create a ScriptableObject for each car. I like this approach if you have a manageable amount of cars. ScriptableObjects will allow you to integrate your Unity workflow with the car data.

c) Have a container of your car prefabs in a manager script, and then have a way to reference each car prefab.

What’s important here is that each car can be referenced and selected in some way. If each car has a unique ID, for example, then you can store a list of just the IDs for the cars the player owns, and then when needed, instantiate from that list the actual car object(s) into the game.

In my opinion, start with the simplest, most straight forward approach if you’re blocked and are unsure. Then update that strategy as your game evolves.

Happy new year and good luck!