Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Question Can I get the "Index Of" node to find an object inside of a Generic List?

Discussion in 'Visual Scripting' started by literacy, Sep 28, 2023.

  1. literacy


    Nov 14, 2021
    I defined a Class called Game State and put another class in it called Activity State.

    I have two graph variables:

    * One is currentActivity, of type Activity State.
    * Another is listOfActivities, which is a List of Activity States.

    I want to look inside of the listOfActivities and get the index number of the Activity State that matches currentActivity. (I know for sure that the item is in the list - just want the index number of it.)

    I am getting an error:


    InvalidConversionException: Cannot convert from 'System.Collections.Generic.List`1[ActivityState]' to 'Unity.VisualScripting.AotList'.

    From what I understand, the Index Of node can not work on generic lists like Activity State. It can only work on AOT lists.

    I have tried two different paths:

    1. Write a custom node that can get an Index Of for a generic list, so I can plug in my List of Activity States directly into that.
    2. Write a custom node that can convert my generic List of Activity States into an AOT list.

    I have failed at both of these attempts. There is a third hacky option:

    3. I could hack a subgraph which just iterates through every item on the list and looks for a match.

    I guess I could do #3, but I feel like there must be a simpler way to access the Index Of an item that I know to be on a generic list.

    Does anyone have advice on how to proceed?
  2. Trindenberg


    Dec 3, 2017
    Use IList -> IndexOf

    literacy likes this.
  3. literacy


    Nov 14, 2021
    Ah wonderful! I found it in my Unity under the name List Interface:


    Seems to be working well, thanks so much!!
    Trindenberg likes this.