C# Custom Resource still in memory but get impossible to cast after being used once.

Godot Version

4.2.2 with C#

Question

Hello !
I’m starting to learn Godot with some C# and I have a weird resource casting issue.
Might be related to garbage collection but I don’t know enough about Godot and don’t remember enough about c# X).

So I just wanna spawn some dominos.

so I have this class :
image
in an array in another custom resource :
image

Everything is created at edit time in the editor and only accessed at runtime.
At some point I need to get the material associated with a type so I call this method twice :
image
which runs fine both times.
And I manage to spawn my first domino.
I can see in memory that the 4 RegionSettings are of the right type in their array :

Now when I wanna spawn the second Domino, I get an exception saying the ressources in my array cannot be casted to my custom type anymore.

Still the same resources names in my settings, but I just can’t iterate on them.

The issue occurs as soon as I exit my first spawn method here :

Any help is appreciated, I don’t get why those pointers would change.
Thanks !

Ewkaly

It’s hard to debug this issue without having access to all the code for your system. Beyond using != instead of IsInstanceValid(), I don’t see anything weird in your code. It does look like you are using some extension methods which are undisclosed.

If you want more than a guess as to what is wrong, please provide more of the relevant code.

ok so I got a bit more info since then.
Commenting the Init Method made it not trigger the exception, so I commented some lines in it and found the culprits :

Before I gave the material in my ressource to some meshes, and it somehow messed with the ressources.
Now I duplicate the Material and I don’t get any issue.
That at least unblocks me, but I still find it a bit worrying that I don’t understand what was happening. ^^’

Even if the mesh somehow needs full ownership of the Material instance, I don’t see why it would invalidate the resource and not just the Material pointer.
For now I just want to color my dominos by type, so sharing the same materials across all meshes seemed fine but maybe they need to be unique ?