Search Unity

Trouble creating class of bools

Discussion in 'Getting Started' started by pod11, Oct 25, 2020.

  1. pod11

    pod11

    Joined:
    Jan 6, 2019
    Posts:
    60
    in my new project im attempting to create array consisting 10 arrays of 10 bools each.
    They way i attempted it is to create a class of 10 bools and then Array consisting 10 of those classes:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System;
    5.  
    6. public class RowOfCells
    7. {
    8.     public bool[] isCellActive;
    9.    
    10.  
    11.  
    12.     void Start()
    13.     {
    14.         isCellActive = new bool[10];
    15.      
    16.  
    17.     }
    18.  
    19.  
    20.    
    21.     public  RowOfCells(bool value1 ,bool value2,bool value3,bool value4,bool value5,bool value6,bool value7,bool value8,bool value9 ,bool value10)
    22.     {
    23.         isCellActive[0] = value1;
    24.         isCellActive[1] = value2;
    25.         isCellActive[2] = value3;
    26.         isCellActive[3] = value4;
    27.         isCellActive[4] = value5;
    28.         isCellActive[5] = value6;
    29.         isCellActive[6] = value7;
    30.         isCellActive[7] = value8;
    31.         isCellActive[8] = value9;
    32.         isCellActive[9] = value10;
    33.     }
    34. }
    35.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Organism : MonoBehaviour
    6. {
    7.     public RowOfCells[] patternOfCells;
    8.  
    9.     void Start()
    10.     {
    11.  
    12.         patternOfCells = new RowOfCells[10];
    13.  
    14.         for (int i = 0; i <= patternOfCells.Length-1; i++)
    15.         {
    16.  
    17.             patternOfCells[i] = new RowOfCells(true, true, true, true, true, true, true, true, true, true);
    18.         }
    19.  
    20.     }
    21.  
    22.  
    23.     void Update()
    24.     {
    25.        
    26.     }
    27.  
    28. }
    29.  
    I am receiving error:
    NullReferenceException: Object reference not set to an instance of an object
    RowOfCells..ctor (System.Boolean value1, System.Boolean value2, System.Boolean value3, System.Boolean value4, System.Boolean value5, System.Boolean value6, System.Boolean value7, System.Boolean value8, System.Boolean value9, System.Boolean value10) (at Assets/Scripts/RowOfCells.cs:23)
    Organism.Start () (at Assets/Scripts/Organism.cs:17)

    I don't iunderstand why, in my understanding i am creating instance of an object.
    Please help what im doing wrong?
     
  2. Neto_Kokku

    Neto_Kokku

    Joined:
    Feb 15, 2018
    Posts:
    1,751
    Start() is a Unity "magic method", which is called on Monobehaviour instances before their first Update() (another magic method). Your RowOfCells class is a plain C# class, its Start() method will never be called, and even if it were, it would never be called before the constructor, so isCellActive is null.
     
    Vryken likes this.