Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by travisc088 · Nov 25, 2021 at 02:29 AM · scriptingbasics

How to store functions in Scriptable Objects

Hello! I recently tried to program a game and wanted to create weapons with different abilities rather than just stats. So while I was searching for answers, I came across this post, which answered how to add functions to scriptable objects. However, I am a little confused about the application. In my project, I have a simple scriptable object called WeaponHandler

 public class WeaponHandler : ScriptableObject
 {
     public string Name;
     public GameObject Object;
     public Sprite Icon;
     //animation
     
     public float Cooldown;
     public float AttackDamage;
     public int Ammo;
     public virtual void OnUse()
     {
         Debug.Log("customize this");
     }
     //on hit
     
 }

And In it, I created a scriptable object called "Katana" in the editor. I tried to mutate the OnUse function by making another class named Katana in the same file and mutating the function like this.

 class Katana : WeaponHandler
 {
     public override void OnUse()
     {
         Debug.Log("Katana");
     }
 }

However, when I call the function from the Katana object it writes "customize this". How would I actually store a custom function in a Scriptable Object?

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

4 Replies

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

Answer by Captain_Pineapple · Nov 25, 2021 at 04:58 PM

So to put an end to this as my original post that you refer to was not clear enough. This is how it is intended to be used and this is how it works (tested).

 using UnityEngine;
 
 public class BaseObj : ScriptableObject
 {
     public virtual void OnUse()
     {
         Debug.Log("Base Obj");
     }
 }
 
 public class Derived : BaseObj
 {
     public override void OnUse()
     {
         Debug.Log("derived");
     }
 }
 
 public class testscript : MonoBehaviour
 {
     public BaseObj EditorAssignedScriptableObjectTest;
     public void Start()
     {
         var baseObj = new BaseObj();
         var derivedObj = new Derived();
         BaseObj baseDerivedObj = new Derived();
         baseObj.OnUse();
         derivedObj.OnUse();
         baseDerivedObj.OnUse();
         EditorAssignedScriptableObjectTest.OnUse();
     }
 }

In ScriptableTest.cs i added this class as well to show that this can be used by loading from resources as well:

 [CreateAssetMenu(fileName = "ScriptableTest", menuName = "ScriptableObjects/testObj", order = 1)]
 public class ScriptableTest : BaseObj
 {
     public string objName;
     public override void OnUse()
     {
         Debug.Log("derived name: " + objName);
     }
 }

Then rightclick in your resources, go to Create -> ScriptableObjects -> testObj

Set some name for the object. Mine looked like this:

alt text

Then you assign that to your testscript behaviour:

scriptable object

This will create the following 4 messages in the following order:

  • Base Obj

  • derived

  • derived

  • derived name: Whatever name you assigned in the obj you created


If this is not how you are using this then please update your question with some more details of how you apply this.


capture.png (7.9 kB)
capture2.png (12.5 kB)
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 travisc088 · Nov 26, 2021 at 03:51 AM 1
Share

Thank You so much! Seeing the code really helped me understand how this works with the Unity engine!

avatar image
0

Answer by ssarfaty · Nov 25, 2021 at 09:21 AM

You could try marking your OnUse function as abstract instead of virtual. Abstract functions only have a definition but no implementation, so calling OnUse from the Katana class should print "Katana".

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 Captain_Pineapple · Nov 25, 2021 at 04:33 PM 0
Share

No. I do not know why this got upvoted. A non abstract class can not contain an abstract function. You'd have to make WeaponHandler abstract for this to work.

avatar image
0

Answer by sacredgeometry · Nov 25, 2021 at 10:06 AM

To answer your question you can add a method (a class function) to an object by declaring it like:

 // Method signature with return type
 AccessModifier ReturnType NameOfMethod(Param1Type param1Name, ...) 
 {
      // Do some stuff here
      return returnTypeInstance;
 }
 
 e.g.
 
 public string GetName(string firstName, string surname)
 {
     return $"{firstName} {surname}";
 }
 
 // Or a method without a return type would use a void return type and would not need to invoke the statement
 
 public void DoSomething(string value)
 {
     // Do something here
 }



You can also use delegates/ lamdas to store functions into variables by declaring them using the Action or Func types Funcs having a return value and Actions being functions without.


That said for this I would probably learn about interfaces (in the loose sense either interfaces or abstract classes) and use a strategy pattern combined with composition i.e. keep the scriptable objects just as lean data only POCOs and then add an instance or collection of them as a member on another class and do all the weapon behaviour logic on that instead:


https://www.youtube.com/watch?v=v9ejT8FO-7I

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

Answer by logicandchaos · Nov 25, 2021 at 10:43 AM

You have things mixed up, the 1st script you create a scriptable object and then you say you make an instance of it called katana, but then you make a new katana script that inherits from WeaponHandler, but your scriptable object instance is still a WeaponHandler not a katana.

To use scriptable object you need to make the the instance of katana script not WeaponHandler script.

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 sacredgeometry · Nov 25, 2021 at 01:13 PM 0
Share

Where are they instantiating anything?

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

139 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

Related Questions

Issue with animation and gravity. 1 Answer

How to make my air plane to fly straight up (its local y-axis is parallel with global y-axis)? 0 Answers

Calibration of accelerometer with offset 2 Answers

2D destroyer moves 0 Answers

Third person controller 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