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
0
Question by GlitterFuzz · Jun 07, 2021 at 10:27 AM · inheritanceoverride

Help with override function in inheritance,HELP with inheritance overide functions

So, I've been attempting to make a spell system using inheritance in unity. I have a Fireball scriptable object created, and residing in a public 'Spell' variable present in the playerscript. The playerscript reads this spell object (the fireball), and calls the cast function of it.

From what I understand, the 'virtual' and 'override' keywords present in my code should make it so that any Child class that I create of the Spell class, should be able to call the Spell Cast() function and have it override to their own specific spell effects.

Testing this out with the Debug.Log and Debug.LogError, shows that only the base.Cast() function is actually called, and never the overridden funtion in the Fireball Class.

This has had me stumped for a while, since I haven't even written any complex code here. Any help or advice would really be appreciated, thanks!

  private void Update()
     {
         if (Targets.Count > 0){
             if (Input.GetKeyDown(KeyCode.Mouse0)){
                 if (Targets.Contains(StaticGrid.selectedCell.gameObject))
                 {
                     Debug.Log("Before Cast");
                     Debug.Log(stateController.selectedWizard.Abilities[Index]);
                     stateController.selectedWizard.Abilities[Index].Cast();
                     Targets.Clear();
                     cam.camState = CameraScript.CamStates.Wizard;
                 }
             }
         }
     }

(Both of the Debugs in the playerscript above run when expected, so I know the code reaches this point as I intend it to. The selectedWizard has a List populated with the abilities that he has at that time. In this case a Fireball at index 0, and a Magic missile at index 1. The second Debug confirms that it is reading the fireball correctly, but still using the function of its parent class)

 public class Spell : ScriptableObject
 {
     //EVENTUALLY ADD ANIMATIONS TO SIT IN THE WINDOW OF THE CARD, SHOWING THE SPELL'S EFFECTS
     [Tooltip("The name of the Spell")]
     public new string name;
     [Tooltip("A Detailed Mechanical Description of the Spell")]
     public string description;;
 
     public virtual void Cast() { Debug.LogError("Cast from default spell class"); }
 }
 
 [CreateAssetMenu(menuName = "Spells/Fireball")]
 public class Fireball : Spell
 {
     public override void Cast()
     {
         Debug.Log("Cast Fireball");
     }
 }
 [CreateAssetMenu(menuName = "Spells/MagicMissile")] 
 public class MagicMissile : Spell
 {
     public override void Cast()
     {
         Debug.Log("Cast Magic Missile");
     }
 }

Comment
Add comment · Show 1
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 Llama_w_2Ls · Jun 07, 2021 at 11:22 AM 0
Share

This might be an issue with how you filled the stateController.selectedWizard.Abilities list. Can you post the full script

0 Replies

· Add your reply
  • Sort: 

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

120 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

Related Questions

An OS design issue: File types associated with their appropriate programs 1 Answer

Inherited class, trying to overwrite variable. 0 Answers

[Solved] Base Update Function? Does It Use Overriden Methods? 1 Answer

Quick Question About Overriding in C# 1 Answer

Practical differences between overriding and shadowing Start(), Update()... 2 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