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 Clofarabine · Feb 22, 2017 at 08:32 AM · objectsclassesmethods

Running a method from a subclass?

I'm a new programmer working on a TCG. I've got a bunch of cards that inherit from a "Card" class. Here's an example, Fireball.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
      
 public class Fireball : Card {
 
     void Awake(){ 
         hand = GameObject.Find("Hand");
         centerSlots = GameObject.Find("CenterSlots");
         fireCost = 0;
         frostCost = 0;
         timer = 2;
         sprite = gameObject.GetComponent<SpriteRenderer> ();
         inHand = true;
         }
         
     void Effect(){
         //effect of card
     }
 
     void OnMouseDown ()
     {
         if (Castable () && inHand) {
             Cast ();
         }
     }
 }

I put all of the methods that I thought all of the cards would use in the Card class, which has been working well. Each card needs to have a separate "Effect" method, which I planned to write in each individual card script. In the game, the cards will take a certain amount of time to take effect. To do this, I used a coroutine in the Card class.

     protected void Cast (){
         PayMana ();    
         RemoveFromHandList ();
         MoveToCenterSlot ();
         AddToCenterSlotsArray ();
         RealignHand ();
         inHand = false;
         inPlay = true;
         StartCoroutine (Timer());
     }
 
     protected IEnumerator Timer (){
         while (timer > 0) {
             timer--;
             yield return new WaitForSeconds (1);
         }
     gameObject.Effect
     Destroy (gameObject);
     }

}

This gives me the error " Type UnityEngine.GameObject' does not contain a definition for Effect' and no extension method Effect' of type UnityEngine.GameObject' could be found. Are you missing an assembly reference?"

I can't reference Fireball in the inspector, because there's going to be lots of different cards as prefabs. I'm still getting used to all of the objects and classes stuff, so maybe I'm making a simple mistake. Anyone have an idea of how to go about this, or happen to notice a mistake I'm making elsewhere? Thanks!

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 CesarNascimento · Feb 22, 2017 at 12:09 PM

You can use Effect as an abstract method. In this case you'd first need to add "abstract" to your Card class, to make it look like this

 public abstract class Card

then add this to Card

 public abstract void Effect();

Now, on Fireball and any other card that you inherit from Card, add "override" just before the return value of the method, like this

         public override void Effect()

and now, inside Timer you just need to call Effect(); and it'll run the right method.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I use JS to access C# methods/classes? 2 Answers

Can I increment a static variable from a static method? 1 Answer

Instances of Classes 1 Answer

How can you list all classes inside a namespace (such as UnityEngine) and every message method that class has? 1 Answer

Class Reference to Other Class does not allow me to use its Methods 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