Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

GetComponent<Type>, possible to make it work?

Discussion in 'Scripting' started by PhoenixRising1, Oct 3, 2015.

  1. PhoenixRising1

    PhoenixRising1

    Joined:
    Sep 12, 2015
    Posts:
    488
    I have made the following:

    Code (CSharp):
    1. public interface Enemy
    2. {
    3. }
    4.  
    5. public class Peasant : MonoBehaviour, Enemy
    6. {
    7. ...

    and I'm trying to get the following to work, but maybe it's just not possible:

    Code (CSharp):
    1.         Enemy other = GetComponent(typeof(Enemy));
    2.         Type enemyTypeScript = other.GetType();
    3.         GetComponent<enemyTypeScript>();

    Please enlighten me :).
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    What's wrong with just GetComponent<Enemy>()?
     
    PhoenixRising1 likes this.
  3. amit1532

    amit1532

    Joined:
    Jul 19, 2010
    Posts:
    305
    That is not a valid c# code.
    GetType returns an instance of System.Type

    Which means:
    SomeType a = new SomeType()
    SomeType is not the same as a.GetType()

    What c# expects to recieve in the angle bracets is a TypeName.

    So GetComponent<SomeType>() is valid
    And not GetComponent<someVariable>() is invalid.

    That being said, there is a constraint to the typename GetComponent allows you to put: it must ve derived from Component class.

    This is why GetComponent<Eneny>() is invalid. Enemy is an interface and interfaces cannot inherit classes, thus Enemy cant inherit Component. And this is why any interface cannot be placed inside the angle bracets.

    What you can do (and already did) is use the other overload of GetComponent that gets the System.Type of the component you ask for.

    Enemy other =GetComponent(typeof(Enemy));

    This will work. You can use other to access the script implementing Enemy in the GameObject
     
    PhoenixRising1 likes this.
  4. PhoenixRising1

    PhoenixRising1

    Joined:
    Sep 12, 2015
    Posts:
    488
    Thanks a lot for this detailed answer :).

    Edit: I actually tried it the way Grozzler suggested as well and it works too:

    GetComponent<Enemy>().SetInterrupted();

    but I prefer to only type:

    other.SetInterrupted();

    for calls, it looks much better :).
     
    Last edited: Oct 3, 2015
  5. amit1532

    amit1532

    Joined:
    Jul 19, 2010
    Posts:
    305
    I see, they must have removed the constraint lately... It didnt work on early version :)
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Unity 5 snuck in a lot of general improvements like this.
     
  7. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    Wow that is huge, i was still using a extension method to do this not knowing that the getcomponent in untiy 5 can work with interfaces just fine.
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Are you still casting after Instantiate as well?

    ;)
     
  9. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Wait, you want to say you don't need to cast an Instantiate anymore?
     
  10. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Why would you, when you can do this?

    Code (CSharp):
    1. GameObject myGameObject = Instantiate<GameObject>(myGameObjectPrefab);
     
  11. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Ah yeah, that is true... Guess it was too late for me yesterday when posting :p Completely forgot it had a generic method as well.
     
  12. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    I knew about the generic instantiate due to intelli sense but being able to get components by interface is huge for how I like to work.