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.
Answer by phil_me_up · Mar 01, 2016 at 01:27 PM
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.
Thank you for your answer. You are right - I should have references in other script. But my problem is, that I have dynamic objects. I create them at runtime so in my case this is not useful I think. Anyway, I have already figured it out. I created really simple script with only one line of code where I declare wheather or not the object is being upgrated or not and save that to PlayerPrefs string value with a name of the object. At start up I am now able to find the name of the object that is being upgrading like that:
string buildingBeingUpgraded = PlayerPrefs.GetString("BuildingBeingUpgraded", "0");
if(buildingBeingUpgraded != "0")
{
objectBeingUpgraded = GameObject.Find(buildingBeingUpgraded);
print ("Upgraded Object Newly Assigned!");
}
But thank you anyway.
Your answer
Follow this Question
Related Questions
Unity Ads - link correct ID 1 Answer
Find child script instance from network identity? 0 Answers
How to track variables by reference.. 0 Answers
How can I load a reference to an int into an int array? (c#) 0 Answers
Reference clock from another script 1 Answer