The type 'T' cannot be used as type parameter 'T' in the generic type or method

后端 未结 1 1700
春和景丽
春和景丽 2021-01-18 14:34

I have the following method:

protected T AttachComponent(){
    T gsComponent = gameObject.GetComponent();
    if(gsComponent == null){
            


        
相关标签:
1条回答
  • 2021-01-18 15:09

    The issue is that the AddObject method returns a Component. You need to tell the compiler that your T is actually a type of Component.

    Attach a generic constraint to your method, to ensure that your Ts are Components

    protected void AttachComponent<T>() where T : Component
    {
        // Your code.
    }
    
    0 讨论(0)
提交回复
热议问题