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

Two questions about "multiple variable lists"

Discussion in 'Scripting' started by Hishicorne, Jun 25, 2017.

  1. Hishicorne

    Hishicorne

    Joined:
    Jun 25, 2017
    Posts:
    29
    [Edit : the first question is resolved but I still have problem with the second ^^]

    Hello everyone,

    First of all I want to apologize about my english, I'm french and I may make many mistakes so sorry.

    Soooo, I have actually a problem and a question. Let's begin with the problem : I have this code right bellow that makes a list with multiple variables ; the GameObject (prefab) of the enemy, the minimal level in wich the enemy appears, and the apparition mode (if the enemy is falling, walking or flying.... It will change where the enemy spawns).

    Code (CSharp):
    1. // List of enemys
    2. public List<GameObject> ListEnemies;
    3.  
    4. public class Enemy {
    5.     public Enemy (GameObject enemyprefab, int levelapparition, EnemyStats.SetUpApparitionMode modeapparition) {
    6.         EnemyPrefab = enemyprefab;
    7.         LevelApparition = levelapparition;
    8.         ModeApparition = modeapparition;
    9.     }
    10.  
    11.     public GameObject EnemyPrefab {get; private set;}
    12.     public int LevelApparition {get; private set;}
    13.     public EnemyStats.SetUpApparitionMode ModeApparition {get; private set;}
    14. }
    15.  
    16. public List<Enemy> EnemiesList = new List<Enemy>();
    17.  
    18. void Start () {
    19.  
    20.     foreach (GameObject e in ListEnemies) { // For each game object on the intial ListEnemies list
    21.         EnemyStats enemy = e.GetComponentInParent<EnemyStats>(); // The script that contain enemy's vars
    22.         EnemiesList.Add(new Enemy(e, enemy.ApparitionLevel, enemy.ApparitionMode)); // We create an enemy in EnemiesList according to Enemy class
    23.     }
    24. }
    (Actually if you need here is the code that is used here in the EnemyStats script : )

    Code (CSharp):
    1. // Ints
    2. public  int ApparitionLevel = 0;
    3.  
    4. // Bools
    5. public enum SetUpApparitionMode { FallingEnemy, WalkingEnemy, FlyingEnemy }
    6. public SetUpApparitionMode ApparitionMode = SetUpApparitionMode.FallingEnemy;
    So I have this problem : when I run the game, I directly have this error : "NullReferenceException: Object reference not set to an instance of an object
    GameEnemySpawer.Start () (at Assets/Script/GameEnemySpawer.cs:31)"

    The line 31 is : EnemiesList.Add(new Enemy(e, enemy.ApparitionLevel, enemy.ApparitionMode));
    I'm pretty sure this doesn't come from the values "e", "enemy.ApparitionLevel" or "enemy.ApparitionMode" (i've tested with premade vars, I mean 0 instead of enemy.ApparitionLevel, "FallingEnemy" instead of enemy.ApparitionMode... And I have still the same result) but I can't figure out what's the problem. So my question is that if you can help me to resolve this...

    Secondly, and I may ask a pretty dumb question because it may be easy, but I want to select randomly one enemy on the EnemiesList that is under a certain ApparitionLevel value... I think it's possible but I don't know how...

    Thank you for reading this and maybe helping me, have a good day !
     
    Last edited: Jun 25, 2017
  2. daxiongmao

    daxiongmao

    Joined:
    Feb 2, 2016
    Posts:
    412
    That error means that one of your variables you are trying to use is null. This is typically a class or component or gameobject. Usually variable.something where variable is null.

    These errors will be very common so you should learn how to determine what the problem is your self.
    The best way would be to debug the code and put breakpoints and then step through your code looking at the values of the variables.

    The second would be debug log to print information to the console.
    You can check for this by doing if(enemy == null) if true you can print an error or else do your code.

    Looking at what appears to the same place as the error you mention.
    Either an item in your list e is null or it can't find the component in the parent and enemy is null.
    From your message you have pretty much given your self the answer.
     
  3. Hishicorne

    Hishicorne

    Joined:
    Jun 25, 2017
    Posts:
    29
    Oooooooh yes I found, thank you for the help, it was that. I used GetComponentInParent instead of GetComponent...