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

Resolved Strange behaviour when trying to access a list from a variable

Discussion in 'Visual Scripting' started by Marou1, Dec 27, 2022.

  1. Marou1

    Marou1

    Joined:
    Dec 13, 2021
    Posts:
    161
    Hi,

    I have a variable that has a empty list:
    upload_2022-12-26_21-33-43.png

    When I try to access any property of that variable, it works fine, but for the list, I get an error:
    upload_2022-12-26_21-38-42.png

    How is that possible to access one property and not the other? UpgradeList is not Null, it's an empty list and Count Items function should return 0.


    The weirdest part is that the game object that has the script and variable was instantiated on the runtime from a prefab that is exist in the scene. However, in that prefab, I get the expected result:
    upload_2022-12-26_21-46-56.png



    Even if you have just a guess, please write it below, because this is driving me crazy.

    Thanks
     

    Attached Files:

  2. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,049
    Looks like the list might not be initialized in C# code.

    Try:

    Code (CSharp):
    1. public List<T> UpgradeList = new List <T>();
    this should also work in newer Unity versions:

    Code (CSharp):
    1. public List<T> UpgradeList = new();
     
  3. Marou1

    Marou1

    Joined:
    Dec 13, 2021
    Posts:
    161
    It solved the issue. Thank you so much! :)