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 Ereghor · Apr 24, 2020 at 03:39 AM · scriptable objectinstancing

ScriptableObject instances modifying their original object

I am using ScriptableObjects as a template for storable items in my project. As I tried adding a shelf life property to them, I've noticed that when that value changes at runtime, the original object from the Resources folder has it altered as well.

How the item instances are created:

   public void AddStorageItem (string itemName, int amount)
         {
             int pos = CheckStorageForItem(itemName);
             if (pos != -1)
             {
                 GlobalVariables.storage[pos].stackSize += amount;
             }
             else
             {
                 AttachedItem newItem = ScriptableObject.CreateInstance<AttachedItem>();
                 if (itemName.Contains("ingr_"))
                 {
                     newItem.item = Resources.Load<StorableItem>("Ingredients/" + itemName);
                     newItem.stackSize = amount;
                     newItem.item.itemName = AlchemixTextEN.ingrNames[newItem.item.namedescRef];
                     GlobalVariables.storage.Add(newItem);
                 }
                 if (itemName.Contains("base_"))
                 {
                     newItem.item = ScriptableObject.CreateInstance<BrewingBase>();
                     newItem.item = Resources.Load<StorableItem>("Bases/" + itemName);
                     newItem.stackSize = amount;
                     newItem.item.itemName = AlchemixTextEN.baseNames[newItem.item.namedescRef];
                     GlobalVariables.storage.Add(newItem);
                 }
                 
             }
         }

The item itself:

 public abstract class StorableItem : ScriptableObject {
     public int namedescRef; 
     public int value; 
     public Sprite icon; 
     public int shelfLife; 
 
     [System.NonSerialized]
     public string itemName;
 }

Every in-game minute I subtract 1 from an item's shelfLife:

 foreach (AttachedItem item in GlobalVariables.storage.ToList())
             {
                 if (item.item.shelfLife > 0) item.item.shelfLife -= 1;
                 else
                 {
                     if (item.item.shelfLife != -1)
                     {
                         GameObject.FindGameObjectWithTag("Canvas").GetComponent<UIManager>().DisplayMessage(null, item.stackSize.ToString() + " " + "units of" + " " + item.item.itemName + " " + "have spoiled!");
                         GlobalVariables.storage.Remove(item);
 
                     }
                 }
             }

Is there a way to prevent the instances from changing the original object or am I better off just using MonoBehaviour for it? Thank you in advance.

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
2
Best Answer

Answer by jkpenner · Apr 24, 2020 at 04:36 AM

Think of a ScriptableObject file as a Prefab. Currently it's like you are editing the prefab directly. You'll need to make an instance of the ScriptableObject (similarly to GameObject Prefabs).

 var instance = ScriptableObject.Instantiate(loadedAsset);
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 Ereghor · Apr 24, 2020 at 07:29 AM 0
Share

Thank you very much. Added an Instantiate over ScriptableObject.CreateInstance() and it works perfectly.

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

126 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

Related Questions

Unity 5.4 Gpu Instancing reduces amount of saved batches to zero 0 Answers

.sharedMaterial = Material is creating an instance? 0 Answers

ERROR WINDOW .ItemWindow (Int32 windowID) 0 Answers

ScriptableObject not saving data to asset 2 Answers

Scriptable Object Tutorial/Use in Inventory System 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