Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by xxluky · Mar 01, 2016 at 09:28 AM · referencingid

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xxluky · Mar 08, 2016 at 09:22 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

36 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges