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 Andreas Hagen · May 21, 2011 at 07:24 PM · valuesendmessagereturn

Sendmessage - return value

Situation:
I'm making a base class from which other classes inherite function, etc. This classes have all different names(of courcse)

Problem:
Now I want to call a function(let's say "SuperFunction()" ) and get a value from it. The script should return the first component that has a GetAmmo() function in it. Is this possible?

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 Bunny83 · May 21, 2011 at 09:58 PM

You can use Interfaces to ensure a common interface that all your weapons have to provide. Your class is still derived from MonoBehaviour but in addition you implement an interface. You can use GetComponent() to get a reference to your attached weapon script.

public interface IWeapon { int GetAmmo(); }

public class MyBaseClass : MonoBehaviour { }

public class Shotgun : MyBaseClass,IWeapon { public int GetAmmo() { return 5; // :D implement it as you need it } }

Somewhere else:

MyBaseClass[] list = GetComponents<MyBaseClass>();
foreach(MyBaseClass O in list)
{
    if (O is IWeapon)
    {
        // Yeah, it's a weapon
        IWeapon weapon = (IWeapon)O;
        weapon.GetAmmo();
    }
}

edit

I was wrong about GetComponent. You can use it also on interfaces, thanks Mike. So it's possible to get the attached weapon only be the common used interface:

IWeapon weapon = GetComponent(typeof(IWeapon)) as IWeapon;
if (weapon != null)
    weapon.GetAmmo();

Unfortunately Unity put a constraint on the generic parameter so you can use it only for types that are derived from Component. Here's the implementation:

public T GetComponent<T>() where T : Component
{
    return this.GetComponent(typeof(T)) as T;
}

Maybe they remove the constraint (where T : Component) in the future. That wouldn't hurt much but it would help to use interfaces ;)

Comment
Add comment · Show 8 · 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 Bunny83 · May 21, 2011 at 10:00 PM 0
Share

This information is supplied without liability. No syntax check ;)

avatar image Andreas Hagen · May 22, 2011 at 10:41 AM 0
Share

I know the syntax of both languages,so you can use whatever language you want^^ I haven't known, that you can use GetComponents in that way. But does this method return also the baseclass or just the child classes?

avatar image Mike 3 · May 22, 2011 at 10:55 AM 0
Share

Not entirely true. GetComponent/s(Type) works fine with interfaces (kinda), GetComponent/s() doesn't

avatar image Andreas Hagen · May 22, 2011 at 11:32 AM 0
Share

And can I also access methods in $$anonymous$$yBaseClass when I make the list of components of this class or just the methods in its childs?

avatar image Mike 3 · May 22, 2011 at 12:10 PM 1
Share

You can use any base class functions to the type you have access to. If you have a $$anonymous$$yBaseClass reference, you only get $$anonymous$$yBaseClass functions. Similarly IWeapon would give you just the IWeapon ones. If you have a Gun class which extended $$anonymous$$yBaseClass and IWeapon, and had a reference to one of those, you could use everything from all three

Show more comments

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Return a value for a class? 1 Answer

How to Show Random.Range in the Console when used in an Array? 0 Answers

My SendMessage is problematic [SOLVED] 1 Answer

transform.position related compile error fixed don't understand why ? 1 Answer

Getting values out of an IEnumerator method (C#) 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