Base Upgrade System in Unity
In many games, players are able to upgrade a base or building to unlock additional features, improve defense, or increase production rates. This tutorial will guide you through setting up a base upgrade system in Unity, allowing for a building to be upgraded to multiple levels with visual and functional changes at each level.
1. Setting Up the Base Structure
First, create a Base
script that defines the properties of your base, such as its level, health, and production rate. Each level upgrade will modify these attributes.
using UnityEngine;
public class Base : MonoBehaviour
{
public int level = 1;
public int health = 100;
public int productionRate = 10;
public void UpgradeBase()
{
level++;
health += 50; // Increase health by 50 with each level
productionRate += 5; // Increase production rate by 5 with each level
Debug.Log("Base upgraded to level " + level);
}
}
This Base
script includes a simple UpgradeBase()
method that increments the level and upgrades the base’s attributes. Attach this script to the base GameObject in the scene.
2. Creating an Upgrade Manager
Next, create an UpgradeManager
script that manages the base upgrades, including checking if the player has enough currency to upgrade.
using UnityEngine;
public class UpgradeManager : MonoBehaviour
{
public Base playerBase;
public int upgradeCost = 100;
public void AttemptUpgrade()
{
if (CurrencySystem.Instance.SpendCurrency(upgradeCost))
{
playerBase.UpgradeBase();
upgradeCost += 50; // Increase the cost for each upgrade
}
else
{
Debug.Log("Not enough currency to upgrade.");
}
}
}
Attach this UpgradeManager
script to a GameObject, assign the base GameObject to its playerBase
field, and set the initial upgradeCost
. Each upgrade increases the cost, making future upgrades more challenging.
3. Setting Up a Currency System
To manage upgrade costs, add a CurrencySystem
as a simple way to track and spend the player’s currency. Here’s an example:
using UnityEngine;
public class CurrencySystem : MonoBehaviour
{
public static CurrencySystem Instance;
public int currency = 500;
private void Awake()
{
if (Instance == null)
Instance = this;
else
Destroy(gameObject);
}
public bool SpendCurrency(int amount)
{
if (currency >= amount)
{
currency -= amount;
Debug.Log("Currency spent: " + amount + ". Remaining: " + currency);
return true;
}
else
{
Debug.Log("Not enough currency.");
return false;
}
}
public void AddCurrency(int amount)
{
currency += amount;
Debug.Log("Currency added: " + amount + ". Total: " + currency);
}
}
Attach this CurrencySystem
to a GameObject in the scene. The singleton pattern allows easy access from other scripts, such as the UpgradeManager
.
4. Updating Visuals and Effects per Level
To make each base upgrade visually distinct, add variations for each level, such as different models or textures. For example:
- Create multiple 3D models for each base level, or prepare different textures/materials.
- Add code in the
Base
class to swap models or materials each time the level is increased.
Here’s a code snippet for updating visuals in the Base
script:
public GameObject[] levelModels; // Assign each level's model in Inspector
public void UpgradeBase()
{
level++;
health += 50;
productionRate += 5;
UpdateBaseModel();
}
void UpdateBaseModel()
{
for (int i = 0; i < levelModels.Length; i++)
{
levelModels[i].SetActive(i == level - 1);
}
}
This code will activate the model that matches the base’s current level and deactivate others.
5. Creating a UI for Upgrades
Next, create UI elements to allow the player to initiate upgrades and track the base’s current level. To do this:
- Create a Canvas with a button labeled "Upgrade Base".
- Attach an
OnClick
event to this button, linking it to theAttemptUpgrade
method of theUpgradeManager
. - Display the base’s level, health, and production rate on the UI to give players feedback on upgrade progress.
6. Testing the Upgrade System
Run the game and click the "Upgrade Base" button. With each upgrade, the base’s attributes should increase, and the visuals should change accordingly. Use the Debug log to verify currency changes and level adjustments.
Expanding the System
This base upgrade system can be expanded by adding:
- New levels with distinct attributes and visual enhancements.
- Resource requirements beyond currency, such as materials or time.
- Special effects for upgrades, like particle effects or sound feedback.
Conclusion
We created a base upgrade system in Unity, featuring dynamic attribute increases, a simple currency check, and UI for upgrades. This system provides a foundation for adding more complex mechanics in your game, such as multi-stage upgrades and distinct visuals for each level.