Home > Blockchain >  An assigned variable in Unity 3D becomes null during code even though nothing is done to it directly
An assigned variable in Unity 3D becomes null during code even though nothing is done to it directly

Time:01-14

I have a building grid system in Unity that is based on the TileMap system that already exists. I have systems for placing objects by clicking, and randomly generating objects that also get placed. Placing an object paints the tile a different color to indicate that it's placed.

I'm trying to make a system where you can remove placed objects, which means that the tilemap also needs to be updated to reflect the removal. This doesn't work at all, and creates some issues that I have never encountered and I have no idea how to solve.

I have a function that paints the tiles, and it goes like this:

public void TakeArea(Vector3Int start, Vector3Int size)
{
    MainTilemap.BoxFill(start, tileName, start.x, start.y, start.x   size.x, start.y   size.y);  
}

This all works for placing, but when I try to do it using an OnClick function, I get some errors. The "start" and "size" are passed on correctly, and when printing the values for the area, I get the correct ones. The problem is with the "tileName" variable this time, as it randomly becomes "Null" when I try to print it, even though in the game itself I an see that the assigned value is not "Null", but it references a tile that should be painted.

I have tried so many things for a couple of hours, but I think I finally found a solution. I made some adjustments to that function so that it's exactly the same as it is when an object is generated, as that already works, so I'm first Instantiating the object with this code:

GameObject oreObj = Instantiate(prefab, poss, Quaternion.identity);

Then I will get the object position and size from there the same way I would as if I was placing the object, so it would be like this:

Vector3Int start = gridLayout.WorldToCell(oreBeingRemoved.GetStartPosition());         TakeAreaWithTile(start, oreBeingRemoved.Size, emptyTile);

When I do all this, I get an error that says

"The variable 'grid' of 'BuildingSystem' has not been assigned. You probably need to assign the grid variable of the 'BuildingSystem' script in then inspector."

The problem is that the variable is assigned. The "grid" is always there in the editor, just like that tile that I talked about was, but it says that it isn't for some reason.

Here's a screenshot:

I have tried rewriting the code so many times, I spent about 10 hours here but nothing seems to be working.

CodePudding user response:

As said in the comments the lines you show do not contain the assignment of the variables or what happens to them in between. Nor does it show how your object is instantiated.

One thing that could be your problem: Prefabs can only be serialized with references to themselves or other Prefabs. References to other objects in the scene will get lost. A fix would be a method that assigns your grid reference in the Awake method of your BuildingSystem class.

  • Related