Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Cryno1000 · Aug 23, 2021 at 08:47 PM · scriptableobjectitemitem pickup

Selecting a variable to modify from a ScriptableObject?

Hey, I have a system that allows the player to collect items to improve their stats. Each item is a ScriptableObject with a couple of fields: name, description, and icon. I also want to be able to choose what the item does when creating the ScriptableObject.

So on the ScriptableObject, I have a couple of options. The first one is what formula it does. For this I just have an enum with add, subtract, multiply, and divide as options. Then I have a float for the modifier (so I would do 50 if I was going to add 50 health).

My issue is with the last one. I need to be able to select what attribute I want to modify while editing the ScriptableObject. If I have like 15 variables I want to modify, I don't want to do this:

 switch (item.name)
             {
                 case "Item 1":
                     maxHealth = maxHealth + 50;
                     break;
                 case "Item 2":
                     movementSpeed = movementSpeed + 10;
                     break;
                 case "Item 3":
                     jumpForce = jumpForce + 10;
                     break;

                 //And so on
             }

Ideally, I'd like to be able to do something like this (where I would be able to define what variable I want to modify from the ScriptableObject)

 switch (item.function)
             {
                 case Item.Formulas.add:
                     variable = variable + item.modifier;
                     break;
                 case Item.Formulas.subtract:
                    variable = variable - item.modifier
                     break;
                 case Item.Formulas.multiply:
                    variable = variable * item.modifier
                     break;
                 case Item.Formulas.divide:
                    variable = variable / item.modifier
                     break;
             }

This way whenever I make a new item, I don't have to go into the script to add a couple of extra lines for it, it just works right away.

The issue is that I don't know the best way about doing this. I could create a string on the ScriptableObject where I type the name of the variable I want to modify, then just refer to them by their name in the script. I'd rather not do this, it feels kinda messy referring to things by their name instead of another option.

Are there any other ways I could do this?

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

Answer by joan_stark · Aug 24, 2021 at 07:02 PM

You can aways create a function, and call that function with the parameter you want. For example:

 //Here you have your scriptable object base Item.
 public class Item : ScriptableObject
 {
     public string name;
     public Sprite icon;
     public string description;
 
     public virtual float Add(float value)
     {
         return value;
     }
     public virtual float Substract(float value)
     {
         return value;
     }
 }
 
 //You create child classes of item, so you can create custom behaviours.
 public class HealthPotion : Item
 {
     public float AddHp = 50f;
 
     public override float Add(float value)
     {
         value += AddHp;
         return value;
     }
     public override float Substract(float value)
     {
         value -= AddHp;
         return value;
     }
 }
 
 public class MaxDamage : Item
 {
     public float AddDamage = 3f;
 
     public override float Add(float value)
     {
         value += AddDamage;
         return value;
     }
 }

And then in your player class: public class Player : MonoBehaviour { //I will assume you have an inventory. public List inventory = new List();

     private float playerHp = 50f;
     private float maxDamage = 10f;
     private void Awake()
     {
         //I can add a health option type since its child of Item.
         inventory.Add(new HealthPotion());
         inventory.Add(new MaxDamage());
     }
 
     private void ConsumeItem()
     {
         //For example purposes
         int selectedSlot = 0;
         playerHp = inventory[selectedSlot].Add(playerHp);
         playerHp = inventory[selectedSlot].Substract(playerHp);
         selectedSlot = 1;
         maxDamage = inventory[selectedSlot].Add(maxDamage);
     }
 }

Feel free to ask if you don't understand something. :P

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

128 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

Related Questions

How can define item type in my item pickup system 2 Answers

How to add item to my inventory when I pick them up? 1 Answer

Item/Buff Selection UI 2 Answers

Items: Class or scriptableObjects? 0 Answers

Item database with override functions 0 Answers


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