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

Passing Generic List as argument

Discussion in 'Scripting' started by Jibidiah, Apr 1, 2012.

  1. Jibidiah

    Jibidiah

    Joined:
    Oct 17, 2011
    Posts:
    76
    I'm getting really confused on this.

    basically I have a function with a for loop in it
    Code (csharp):
    1.  
    2. function SwitchViewedUniverse (universe : List) {
    3.     for( var universeSelected :GameObject in (universe)) {
    4.         //do stuff with universe
    5.     }
    6. }
    And then from elsewhere I'm trying to pass a list to the function
    Code (csharp):
    1. SwitchViewedUniverse(multiverseUniverse1);
    The list is defined as
    Code (csharp):
    1.  var multiverseUniverse1 = new List.<GameObject>();
    I'm just trying to pass the Generic List to the function, so it can be used as the list in the for loop. This way I don't have to keep copying and pasting the for loop everywhere each time I want to use it for different lists. But I'm getting errors of:

    (for trying to pass the List as an argument)
    The best overload for the method MultiverseTesting.SwitchViewedDimension(System.Collections.Generic.List.<T>)' is not compatible with the argument list '(System.Collections.Generic.List.<UnityEngine.GameObject>)'.

    and (from the for loop itself)
    BCE0022: Cannot convert 'T' to 'UnityEngine.GameObject'.


    Any help would be great
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Code (csharp):
    1.  
    2. function SwitchViewedUniverse (universe : List.<GameObject>) {
    3.  
    4.     for( var universeSelected :GameObject in (universe)) {
    5.  
    6.         //do stuff with universe
    7.  
    8.     }
    9.  
    10. }
    11.  
     
  3. Jibidiah

    Jibidiah

    Joined:
    Oct 17, 2011
    Posts:
    76
    woo, thanks! You saved me there. If only this were unity answers so I could give you correct answer points.