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 el-RERS · Nov 22, 2014 at 04:36 AM · ienumeratorinterface

I'm not fully understanding the concept of "interfaces" and what's up with the IEnumerator interface...

So I'm new with C# and unity and I thought it would be a good idea to take a deep and hard look at every single one of the scripting tutorials... and I'm kind of stuck on this one: http://unity3d.com/learn/tutorials/modules/intermediate/scripting/interfaces.

From what I can understand, interfaces are kind of like "templates" that you can make to "force" your classes to have specific variables and/or functions... for example, I can have two classes, say class Enemy and class NPC, and I want both of them to be able to be killed, so I can create an interface to "force" both of them to have what is necessary for it, kind of like this:

    public interface IKillable
     {
     public float life;
     public void Die();
     
     }
 
 public class Enemy: Ikillable
 {
 public float life{   } //forced to put this
 public void Die(){   } //forced to put this
 }
 
 public class NPC: Ikillable
 {
 public float life{   } //forced to put this
 public void Die(){   } //forced to put this
 }

Everything great thus far...... BUT.... what's up with "IEnumerator" then? Keep in mind I understand perfectly how to use Coroutines and the yield statement but what I don't understand is how an interface can be a return type for a function, which I assume is what happens when using coroutines. If having a function return an "IEnumerator" is possible, is it also possible for me to create a function somewhere that returns my "Ikillable" interface? if so... what would it be returning or what would it be its purpose? since my interface only has empty shells of variables and methods?

I know even the question itself is confusing as hell but I hope I was clear with what my doubt is... thanks in advance for any help with this! :)

Comment
Add comment · Show 2
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 Kiwasi · Nov 22, 2014 at 04:45 AM 0
Share
  • for viewing all the tutorials and asking questions as you get stuck. The quality of this site would be dramatically improved if everyone did that.

avatar image el-RERS · Nov 22, 2014 at 05:46 AM 0
Share

thanks for the insightful answers!

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Kiwasi · Nov 22, 2014 at 04:41 AM

Welcome to polymorphism. A coroutine can return any object that implements the IEnumerator interface. IEnumerator is kind of a special case, so I'll focus on Ikillable.

It would be perfectly possible for you to define a method that returns an Ikillable. This would mean your method can return any object that implements Ikillable. You can then create a variable of the Ikillable type to store the variable. You can call the methods that belong to Ikillable on it. All of this without ever knowing what the actual object was.

Its very neat, very powerful, and one of the fundamental pillars of object orientated programming.

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 el-RERS · Nov 22, 2014 at 04:58 AM 0
Share

wow that was fast! thank you so much!... however, if I understand you correctly, it means I could do something akin to this:

 Ikillable my$$anonymous$$illableVariable = new Ikillable();
 
 my$$anonymous$$illableVariable.Die();
 my$$anonymous$$illableVariable.life;
 

but how would the program know which Die() function or life variable I'm refering to? weather it is the one in the Enemy class or the NPC class? thank you sooo much! and sorry for the noob questions :/

avatar image Kiwasi · Nov 22, 2014 at 05:10 AM 0
Share

You can't call new on an interface. New invokes the constructor. Interfaces cannot implement any methods, including a constructor.

You could do this. (Pseudo code)

 public class Enemy: Ikillabable {...}

 // In another class
 Ikillable my$$anonymous$$illableVariable = new Enemy;
 my$$anonymous$$illableVariable.Die();
 my$$anonymous$$illableVariable.life;
avatar image el-RERS · Nov 22, 2014 at 05:29 AM 0
Share

oooooooooohhhhhhhh ... thank you!... but... bear with me please :( ... does that mean this would be valid? :

     Ikillable SomeFunction()
         {
         
         return Enemy;
         
         }
     
     Enemy myVar = SomeFunction(); //is this even syntactically correct? :S
 
 myVar.life;



also I assume the "yield" statement is some kind of "return object-of-a-class-that-uses-the-IEnumerator-interface" statement then? again, thanks a lot, being self-taught can be a pain when no one is around to ask questions :/

avatar image Kiwasi · Nov 22, 2014 at 05:37 AM 0
Share

They syntax is correct, but it would not compile. The reason is because the compiler cannot guarantee that the value returned by SomeFunction is an Enemy.

The compiler knows that every Enemy is an Ikillable. However not every Ikillable is an enemy. You could get around this by casting, as follows. However casting in this manner is typically not good practice.

  Ikillable SomeFunction() {
      return Enemy;
  }
  
  Enemy myVar = (Enemy)SomeFunction(); 

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

27 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

Related Questions

Calling IEnumerators 1 Answer

Freaky - can't return; out of an IEnumerator 1 Answer

Respawn Time 1 Answer

trying to run a co routine function gets error 1 Answer

Why Won't My Coroutine Yield? 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