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 yoger · Feb 19, 2014 at 03:56 PM · classinstancemethoddifferent

How to set different methods for instances of object

Here's the thing. I'm doing spells for my game. There's general class Spell that contains all the propierties like Name, Damage etc. There's also a method Cast and I'd like to overwrite it while creating instances of Spell so I can get different result while calling Cast. What are my options?

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

2 Replies

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

Answer by b_chrome · Feb 19, 2014 at 04:01 PM

You have a couple of choices:

Derive a class from Spell for each of the subsequent spells you want to create and make them override the Cast method (which you mark as virtual in the Spell class).

   public class Spell /* : ScriptableObject // if you want to make it available as an asset */ {
       public string name;
       public int level;
       public virtual void Cast(Transform source, Transform target) { 
       }
   }

   public class FireBall  : Spell {
        public override void Cast(Transform source, Transform target) {
           //Make a fire ball and fire it
        }
   }

You could also use a delegate to which you supply a function:

     public class Spell : MonoBehaviour {
          public System.Action<Transform, Transform> Cast = delegate {};
     }

     public class FireBall {
             public void Initialize(Spell spell) {
                 spell.Cast = (source, target) =>{
                     //Cast your fireball
                 };
             }
     }
  
Comment
Add comment · Show 4 · 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 MakeCodeNow · Feb 19, 2014 at 04:29 PM 0
Share

These are great concrete examples. The first one is what I was referring to above. The delegate approach is also totally valid, and is very similar to how virtual functions actually work. The first example may be slightly faster than the second, because delegates can be chanced at run time but virtual functions cannot.

avatar image yoger · Feb 19, 2014 at 04:46 PM 0
Share

Great. I'm actually trying the delegate method. There's one problem. When I try to define Cast method the way you used at this (I guess it's calledlambda expression). Compiler says that "not all code paths return a value". But this Action just takes one argument and shouldn't return anything (it's void).

avatar image b_chrome · Feb 19, 2014 at 04:48 PM 0
Share

You are using Action not Function right? An Action shouldn't need a return value - which line is it on?

avatar image yoger · Feb 19, 2014 at 04:55 PM 0
Share

Alright, my bad. I checked entire method and finally figured out that error came actually from the class instantiating new Spell. Problem solved. Thank you very much :]

avatar image
1

Answer by MakeCodeNow · Feb 19, 2014 at 04:01 PM

This is a texbook case for subclassing/polymorphism/virtual functions.

Assuming you're working in C#, this doc should get you started.

PS - Once you have subclasses, you'll probably also want to make your Spell class abstract (so that it can't be instantiated) as well as the Cast function (to force all subclasses to implement a Cast).

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

20 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

Related Questions

Is every Script I drag on a Gameobject an instance? 1 Answer

How to call a new instance of a class in C# 1 Answer

help with creating a static Instance in javascript 2 Answers

How can I use method to return custom classes (Same Class Type),How to return classes in method 1 Answer

C# Inheritance, base class attributes, override and null object 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