I couldnt describe the question in title please come in and check(im noob)

Hi guys im working on lots of different classes as different objects, and it all inherts something from the class ObjectPrototype

public abstract class ObjectPrototype{
    private string walkable = consts.walkable_rejectall;//whether the object is walkable, returns a type?
    private Owner owner = new Owner();//the owner of the object
    private string type = null;//the type of the object
    private Position pos; //Position of the object
    private bool destroyable;//Whether its destroyable or not
    private int hits, hitsMax;//Its hits(health), and maximum hits(health)
    public abstract void run();//special funciton of the object, customizable
    public string getType(){
        return this.type;
    }
    public string iswalkable(){
        return walkable;
    }
    Position getPosition(){
        return new Position(pos.x,pos.y);
    }
}

And i tested some features on the “child” classes(? or something idk how to call them), such as the TerrainWall:

public class TerrainWall: ObjectPrototype{
    private string type = consts.type_twall;
    private bool destroyable = false;
    public override void run(){}
}

I created a TerrainWall class outside like this:

TerrainWall blank_twall = new();//blank terrain wall used to replace

For just making it a blank terrain wall which i can use as a template of the class itself later such as replacing something with it, but somehow it did not work, and when i blank_twall.getType() it gets the “null” type from the ObjectPrototype, even though i think i declared it in the TerrainWall.
First time i met this i wasnt so sure it was following ObjectPrototype’s type, so i change the default type for ObjectPrototype to “unset”, and if i debug.log(blank_twall.getType()) is gives me “unset”

Can anyone help me why its not declaring the type in the TerrainWall class?
Or any other suggestion thats related to improving my code would be nice, thank you :slight_smile:

The answer is always the same… ALWAYS!

How to fix a NullReferenceException error

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

NullReference is the single most common error while programming. Fixing it is always the same.

Some notes on how to fix a NullReferenceException error in Unity3D:

http://plbm.com/?p=221

I’m not sure how C# handles a case where the same class parameter is defined in both the parent class and the derived class. I think you will have better luck by making the “type” field protected and set the value in the TerrainWall constructor.

I ran a quick test with the code below and I got the expected value from getType() when I made a TerrainWall object.

public abstract class ObjectPrototype
{
    protected string type = "ObjectPrototype";

    public string getType()
    {
        return this.type;
    }
}

public class TerrainWall : ObjectPrototype
{
    //The constructor will be executed whenever you create a new TerrainWall object with new()
    public TerrainWall()
    {
        type = "TerrainWall";
    }
}

Just a side note: using constructors and new() is fine for standalone classes like this, but I don’t think you can do this if your class derives from Monobehavior.

1 Like

Thanks for replying, but i have already the simular idea, i know which variable is null but just not why its null. :sob:

Yay i tried this on it and it worked!!! thank you so much