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 Karishi · May 07, 2021 at 06:39 PM · callmethods

Generalized call of specific methods

I have a script for dragging around cards, and right now each card has its own method - named DoTheThing() - which does that card's individual thing. At present these scripts each have their own if statement:

 public void Update()
 {
     if (Input.GetKeyDown(KeyCode.Return) && isHolding.cardObject == this.gameObject)
     {
         DoTheThing();
         gc.TurnCleanup();
     }
 }

And this is...fine? I guess? I mean, it works. But it seems this could be refactored onto a single script on the play area that calls the DoTheThing method from (current card sitting there) if there were a reliable way to (a) get a reference to the individual script and (b) convince the computer that the reference holds the DoTheThing method.

 System.Type.GetType(GetComponentInChildren<Card>().name).DoTheThing();

This is the line of code I attempted, and I think it fulfills condition (a) but fails at (b). I'm pretty sure it is actually targeting the correct thing - The first child object it'll find will be the card, which bears the same name as its attached script because that's how I attach the scripts at runtime. But it won't compile, because it can't be assured that the type I'm grabbing is actually a script with a DoTheThing method attached. Does anyone have a clever way of getting a whole system of card scripts - all of which do their own things - to activate from a central, generalized "when this is on the play zone, press enter" command, without each card checking for that every frame?

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

Answer by Llama_w_2Ls · May 07, 2021 at 07:13 PM

Use interfaces. They allow you to enforce a script to implement certain methods or properties, and so the compiler is certain that they exist. For example:

 public interface ICard
 {
      void DoTheThing();
 }



Now, any class that inherits from 'ICard', must implement the method 'DoTheThing', which you can see here:

 public class MyCard : MonoBehaviour, ICard
 {
      // This method must exist, or else compiler will throw errors
      public void DoTheThing()
      {
           Debug.Log("I did the thing");
      }
 }



The great thing about multi-inheritance, is that now, any class inheriting from ICard, can have its 'DoTheThing' method called, without having to know the type of the class. For example: instead of GetComponent<Card>().DoTheThing, you can just write: GetComponent<ICard>().DoTheThing. It works with every type, inheriting from ICard, as above.

@Karishi

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 Karishi · May 10, 2021 at 10:56 PM 0
Share

I followed this advice and cross-referenced with a couple tutorial videos to make sure I was doing it right, but...Yeah, it really is as easy as you make it look. What an excellent tool to add to one's program$$anonymous$$g options!

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

117 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

Related Questions

How to create a custom "On..." method? 2 Answers

Queue methods. 0 Answers

Calling a variable based on a variables name? 1 Answer

Variable Not Changing In Method 1 Answer

Trouble calling a function... 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