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 /
avatar image
0
Question by soco2211 · Aug 26, 2018 at 06:14 PM · editorprefabbeginnerbehaviour

Changing prefab value during runtime

I'm making a turn-based battle game, where you run around an overworld map until you get into a random battle and then you're teleported to the battle scene. Basic old school rpg stuff. At certain points in the game, your character learns new magic abilities or can equip new gear so they are added to a list that I keep on a GameManager object. My problem is that when I stop running the game, the prefabs keep all the stuff that I acquired while testing the game. An example, they start with no gear on so the list is empty, but once I equip gear and then end the test run, the gear stays on the list and I have to manually remove it in the editor. Why is it changing the prefab instead of the instance of the prefab? I don't know if it's relevant, but whenever I start a battle, I instantiate the players with hp/mp values that I saved in my GameMgr from the last time I finished a battle.

edit: added some code to maybe help find the problem

So this is the code that Instantiates the player

         for (int i = 0; i < GameMgr.instance.herosInParty.Count; i++) {
             GameObject newHero = Instantiate(GameMgr.instance.herosInParty[i], heroSpawnPoints[i].position, Quaternion.identity) as GameObject;
             newHero.transform.Rotate(0, -90, 0);
             newHero.name = newHero.GetComponent<HeroStateMachine>().hero.baseName;
             herosInBattle.Add(newHero);
         }

This is an example of something that gets changed during runtime and doesn't get reset to default after ending

                 for (int i = 0; i < player.hero.equippedGear.Count; i++) {
 
                     //if gear slot matches, remove old gear, add new gear
                     if (Inventory.instance.GetItem(player.hero.equippedGear[i].itemID).gearSlot == item.gearSlot) {
                         player.hero.equippedGear.Remove(player.hero.equippedGear[i]);
                         player.hero.equippedGear.Add(item);
                         gearSlotPanel.transform.GetChild(1).GetComponent<Text>().text = item.name;
                         player.hero.currDEF = def;
                         break;
                         //if gear doesn't match, check for another iteration, add gear on last iteration
                     } else {
                         if (i < player.hero.equippedGear.Count - 1) {
                             continue;
                         } else {
                             player.hero.equippedGear.Add(item);
                             gearSlotPanel.transform.GetChild(1).GetComponent<Text>().text = item.name;
                             player.hero.currDEF = def;
                             break;
                         }
                     }

I was looking at writing a method that will initialize all the values to their default values when you run the game, it's just a lot of values to set and I'd have to write it for the four different characters.

Comment
Add comment · Show 3
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 eses · Aug 26, 2018 at 06:58 PM 0
Share

@soco2211 - Same as with materials, ScriptableObjects - you are basically manipulating asset in editor. See threads here in answers, it has been discussed before. Edit: "prefab ins$$anonymous$$d of the instance of the prefab?" - this made me think, did I understan your question incorrectly... I'm not quite sure, where, or if you are actually using prefabs directly or not.

avatar image kaliAJ eses · Jun 16, 2020 at 01:22 PM 0
Share

@eses @tormentoarmagedoom @soco2211 @qtoompuu

I am facing a similar issue, changing the prefabs in runtime should not alter the prefabs permanently once the game is stopped playing Right? but they are getting changed why is that happening? please help

avatar image tormentoarmagedoom · Aug 26, 2018 at 07:54 PM 0
Share

can you show how are you storing /loading and instantiating the prefabs?

code! we are not magicians!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by qtoompuu · Aug 26, 2018 at 08:50 PM

Without seeing any code, it is hard to know exactly what is causing the problem. But,l based off of what you've said, you could try to introduce a script that resets the character when a new game is started. Post the code and I will try to offer a more in depth solution.

Comment
Add comment · 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

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

150 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I create a grid for a Leveleditor? 1 Answer

[Bug?] Prefab Instantiates with wrong public variables 1 Answer

Change angle of camera in prefab preview 1 Answer

Attempting to write an editor tool to let me place random prefabs frrom a pool in the scene editor on mouse click. It's causing issues! 0 Answers

How Mark Prefab Dirty? 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