Error message received when trying to define a List

Hi,
This is my first question. Sorry if it is in the wrong place.
I am following the instructions for Unit 5 of “Create with Code”

I have tried several ways to define a List of GameObjects to no avail, for any variation I receive an error message.
For example, for

    // public static List<GameObject> targets = new List<GameObject>();

I receive the following error message:

Assets\Scripts\GameManager.cs(6,12): error CS0246: The type or namespace name ‘List<>’ could not be found (are you missing a using directive or an assembly reference?).

It seems “List” is not available anymore. I have also tried using NativeList but I also receive error messages

Are you correctly using the assembly that comes from?

Always start with the docs for what you want, in this case List<T>()

Also, remember: NOBODY here memorizes error codes. That’s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

The complete error message contains everything you need to know to fix the error yourself.

The important parts of the error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)
  • also possibly useful is the stack trace (all the lines of text in the lower console window)

Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly? Are you structuring the syntax correctly? Look for examples!

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

Your script is likely missing the using System.Collections.Generic; at the top. Which is odd as every default script should start with it.

Hello, So as said using System.Collections.Generic; would work.
As of Unity6 the scripts in the editor are split into categories, they also remove auto-implementation of the using System; set of libraries.

IF you get conflict between a system library and unity engine library, as can be the case if both libraries contain the same name for a method, ensure you specify which (using Random.Range() as the example) you would write UnityEngine.Random.Range()

Another solution instead of a list could be an array, which is easily implemented and adapted within the engine. Then cycle through the array using for loops.

Yeah I did notice when playing around in the latest Unity 6 that the using System.Collections; and the using System.Collections.Generic; have been removed from the default monobehaviour and scriptable object templates.

I think this is a HUGE MISTAKE and both default scripts should include these fundamental imports again.

1 Like

@Kurt-Dekker , @spiney199 ,

Thanks for your answers. I installed a previous version of Unity and the problem was solved. Maybe it was not the best way to do that, but I am re-learning after a looooong hiatus of some 12 years. Glad to be back.

You didn’t need to install a whole new version, you just had to add using System.Collections.Generic; at the beginning of your script.

1 Like