Search Unity

I broke my project a little bit - Systems.Collections.Generic HELP

Discussion in 'Getting Started' started by mangapinches, Sep 28, 2019.

  1. mangapinches

    mangapinches

    Joined:
    Apr 2, 2019
    Posts:
    12
    So I was trying to add lists to my CharacterController2D code to store some data.

    I was being forced to use ArrayList but wanted to use List so I added Systems.Collections.Generic to the top of the project.

    Then the IENumerator function I had from before but didn't need was throwing an error. It also greyed out the Systems.Collections at the top, since I had the Collections.Generic (I guess?) so I removed the Systems.Collections. After that, the List <> worked fine but when I ran the game I had these errors:

    NullReferenceException: Object reference not set to an instance of an object
    UnityEditor.Graphs.Edge.WakeUp () (at C:/buildslave/unity/build/Editor/Graphs/UnityEditor.Graphs/Edge.cs:114)
    UnityEditor.Graphs.Graph.DoWakeUpEdges (System.Collections.Generic.List`1[T] inEdges, System.Collections.Generic.List`1[T] ok, System.Collections.Generic.List`1[T] error, System.Boolean inEdgesUsedToBeValid) (at C:/buildslave/unity/build/Editor/Graphs/UnityEditor.Graphs/Graph.cs:387)
    UnityEditor.Graphs.Graph.WakeUpEdges (System.Boolean clearSlotEdges) (at C:/buildslave/unity/build/Editor/Graphs/UnityEditor.Graphs/Graph.cs:286)
    UnityEditor.Graphs.Graph.WakeUp (System.Boolean force) (at C:/buildslave/unity/build/Editor/Graphs/UnityEditor.Graphs/Graph.cs:272)
    UnityEditor.Graphs.Graph.WakeUp () (at C:/buildslave/unity/build/Editor/Graphs/UnityEditor.Graphs/Graph.cs:250)
    UnityEditor.Graphs.Graph.OnEnable () (at C:/buildslave/unity/build/Editor/Graphs/UnityEditor.Graphs/Graph.cs:245)

    I'm pretty new to coding and have only taken some Unity game courses online. Is there a simple fix for this?

    Right now I have both Systems.Collections and Systems.Collections.Generic at the top of the project - but they are now both greyed out. I'd really prefer to keep using List<> since I don't know what ArrayList is and I have another C# code that is passing in data via List<> already.
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,195
    My recommendation is to clear the console. If it persists then it's an actual error in your code.
     
  3. AndersMalmgren

    AndersMalmgren

    Joined:
    Aug 31, 2014
    Posts:
    5,358
    ArrayList is kept for backward compatibility only. It should not be used, if used with value types it will even allocate because of boxing
     
  4. mangapinches

    mangapinches

    Joined:
    Apr 2, 2019
    Posts:
    12
    This is the only error being thrown now in the Console:

    It references line 136 in my CharacterController2D script where I make an argument for an If statement:

    Code (CSharp):
    1. if (SkillCardInventory.instance.skillCards[0].skillRankProgression != 0)
    2.                 {
    3.                     skillRankProgressionForMovement = SkillCardInventory.instance.skillCards[0].skillRankProgression;
    4.                     _vx = Input.GetAxisRaw("Horizontal");
    5.                 }

    I know I'm definitely screwing this up somehow since I'm completely new to using Lists and frankly a bit befuddled concerning their use.

    By the way, what I'm trying to do with the if statement is to make sure that when the character receives the data from the list (basically movespeed), then I allow the horizontal movement to occur. If the list is unable to retrieve the data, the default value for skillRankProgression is zero.

    I think it's throwing the error because the List starts out empty. Then when I try to move there is no data for the argument (if statement). After equipping something to the List, it actually works. Still not sure how to stop the console from freaking out about the empty list.

    SOLUTION:
    Instead of looking for a value that is not there in the List - I switched the if statement do to List.Count > 0. This stopped the freakout problem :)
     
    Last edited: Oct 3, 2019
    JoeStrout likes this.
  5. Deleted User

    Deleted User

    Guest

    You are accessing position `0` in your array. If the array is empty then referencing position `0` will throw an `IndexOutOfBoundsException`.