How to find object that is being upgrated after the game reloads?

Hi guys,

I searched the internet but could not find anything to my subject.

I am working on a game like Clash Of Clans. But for now, I am working on upgrades. When I click an Object, button shows up for upgrating. When I clik the button, I call a function and pass some parameters like GameObject reference of what object should be upgraded. Upgrade starts and keep runninig even when I close the game.

But here is my problem: when I reopen the game I have to pass the reference of the upgrating object manually like this: public GameObject objectBeingUpgraded;

How should I do this automatically? I wanted to use Gameobject.Find but it is slow and even more - I have more objects named for example Sniper with the same name that are being instantiated during runtime while adding new buildings. What about IDs? But I am only begginer in programing.

Please, help.

Generally speaking, when performing upgrades etc you don’t care about the game object / monobehaviour etc. What you do care about is the data behind it.

You’ll need some sort of manager class which is always active in your game and which has a list of all possible upgrades and their status. You’ll need to load in and save out this information when your game starts and ends.

The gameobject which then represents the upgrade can look at the data in this management class and set itself up / update itself accordingly.

If your very new to programming then these concepts might seem a bit odd, but the idea is that you have one point of reference for all your upgrade data, not lots of points in many different game objects.

Something like the following is a basic start (warning, I’ve just typed this here without checking it so expect a few errors if you copy paste). There is a lot you can do to improve upon it.

public class UpgradeManager : MonoBehaviour
{
[System.Serializable]
public class Upgrade
{
public string upgradeId;
public int upgradeLevel;
public bool isUpgrading;
public DateTime completionTime;
}

public List<Upgrade> upgrades = new List<Upgrade>();

void Awake()
{
...
}

void Start()
{
...
}

void Update()
{
...
}
}

Your game object which now wants to look at the upgrade level can do so by querying the list of upgrades defined about and determine for a given id, when the upgrade will finish.

As you are storing a completion time (which you would set when starting the upgrade), you will at any point in time know how long it is till the upgrade is complete and as long as you properly save and load this data into the game, it will persist over multiple play sessions.