0
\$\begingroup\$

I want to to have multiple prefabs like canon level 1 and then level 2.

When the player has enough coins, they can upgrade it to level 2.

I want to make a game similar to Clash of Clans so I will have many buildings that can go through different levels.

How can I do this?

\$\endgroup\$
8
  • \$\begingroup\$ Yes, of course this is possible. If you can imagine a game mechanic, then you usually can implement it in Unity. But there are lots and lots of ways to do this. Which one is "the best" depends on your game. Can you be more specific about which problems you face while implementing this yourself? \$\endgroup\$ – Philipp Feb 1 at 10:01
  • \$\begingroup\$ @Philipp I aim to remake clash of clans in a way or try to implement an upgrade system like the one they have \$\endgroup\$ – elliot727 Feb 1 at 10:05
  • 1
    \$\begingroup\$ I never played Clash of Clans. Can you explain it in your own words? And what aspect of it exactly do you have trouble with? The UI to handle all of that? The mechanics which determine when the player can upgrade? Or replacing the entities with a better version of them? \$\endgroup\$ – Philipp Feb 1 at 10:06
  • \$\begingroup\$ @Philipp well I have no clue how I even start I know how I do the ui but not he mechanics. As for clash of clans here it is supercell.com/en/games/clashofclans \$\endgroup\$ – elliot727 Feb 1 at 10:08
  • 1
    \$\begingroup\$ No, I am not going to play that microtransaction cash grap. Please describe it in your own words. \$\endgroup\$ – Philipp Feb 1 at 10:16
3
\$\begingroup\$

One way to do this (there is never just one way to do something) is to simply replace the old building with a new one by Destroy()ing the gameObject of the old building and then Instantiate()ing one of the next level with the same position, rotation and parent.

One drawback of this method is that the building loses all of its internal state when you upgrade it. If that is a problem, then you might want to create two separate gameObjects for each building with a parent-child relationship. The parent persists during upgrades and carries all the functionality which is independent from the upgrade level of the building. The child which contains all the components where the upgrade level matters (including the SpriteRenderer/MeshFilter that stores the appearance). That way you can replace the level-dependent child but retain the level-agnostic parent.

I could also imagine several other solutions to the problem of managing state during upgrades, all with their own advantages and disadvantages. But I don't know what your requirements are, and I don't know how familiar you are with more advance programming concepts (like ScriptableObject's or C# Attributes). So I will leave that for another question.

Storing the prefab for the next upgrade level

You could either implement this on a global manager-object which has prefab-references to all upgrade levels of all buildings and also contains the information what's the next upgrade level for each building. Or you could implement it on the buildings themselves. Give the script that handles the upgrading a reference to the prefab for the next upgrade level. Both could work, and which one you choose is mostly a question of how you prefer to structure your project.

Getting the cost of the next upgrade level

In either case, it is possible to access variables of an uninstantiated prefab. It's rarely a good idea to change variables of prefabs at runtime, but there is no reason to not read them. So you can put the cost for upgrading a building into a public variable on the prefab for the next upgrade level.

So you can simply add a bool CanUpgrade method to the script which handles the upgrading which compares the cost of the prefab for the next upgrade level to the current resource count of the player. Then the script which handles the UI can use that method to determine if it should enable or disable the upgrade button.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.