0

I have a class Array2DBool. I am trying to change it's values on initialisation. I've tried different approaches but making a constructor for it seems to be the best one so far. Tried testing different parameters: strings, bools and ints. But all leads to the same result - when instantiating I always get "Here" in console and the second constructor just never gets invoked.

namespace Array2DEditor
{
    [System.Serializable]
    public class Array2DBool : Array2D<bool>
    {
        public Array2DBool () {
            Debug.Log("Here");
        }

        public Array2DBool (int gridSizeInt) {
            Debug.Log("Finally Here");
            GridSize = Vector2Int.one * gridSizeInt;
        }
    }
}

// This line is used in another file.
public Array2DBool exits = new Array2DBool(3);

Also, Array2D is an abstract class but it does not have any constructors. So I believe it does no matter since whatever happens there will not affect the constructors here.

  • Are you sure? Are you editing the correct file? Are you compiling all your source files correctly? Did you try to run a clean build? – knittl Jan 01 '23 at 19:22
  • 2
    The code as you've written it here isn't capable of exhibiting the behavior you describe. Either the code isn't what you think it is, or the relevant bits are not what you've copied here. Try creating a [mre]; in the process it's likely you'll find the issue yourself. – Jeroen Mostert Jan 01 '23 at 19:27
  • 1
    Is this Unity? If yes, please add the corresponding tag. As for the issue - please verify that you are running the latest version of code. – Guru Stron Jan 01 '23 at 19:49
  • 2
    The `[System.Serializable]` suggests that the parameterless constructor is being invoked by a call to `IFormatter.Deserialize()` somewhere, rather than your `new Array2DBool(3)`. Set a breakpoint on the parameterless constructor and take a stack trace to see who is calling it. – Raymond Chen Jan 01 '23 at 21:38
  • Are you sure, that class with `public Array2DBool exits = new Array2DBool(3);` was instantiated? You don't have anywere initialization of `new Array2DBool()` ? You can try to remove constructor without parameters. – Alexandr Jan 01 '23 at 21:54
  • Not true about base classes: https://stackoverflow.com/q/13166019/1690217 – Chris Schaller Jan 02 '23 at 02:29

0 Answers0