Destroying assets is not permitted to avoid data loss

Full transparency, I know I am in over my head. I am making a Flappy Bird clone using visual scripting in the latest version of Unity. I have a Pipe Prefab that instantiates off-screen and moves left. I want to destroy this clone when it goes off-screen, like -45. I have found an answer to the issue ( Error on visual scripting : Destroying assets is not permitted to avoid data lost ) but I suspect my implementation of the advice is missing something simple.

You are trying to destroy the prefab asset, not its instance. Cache your instantiated prefab in a new GameObject variable, and destroy that instead.

1 Like

I’ll look into how to cache a prefab, thanks! In looking for a solution it referenced how using a pool is better than destroying and creating the prefab clones. Does that sound like a better avenue?

You take the output of Instantiate node and Set Variable with it, which you already are doing in Start, I didn’t notice. The issue is that the Update runs before your PipePrefab is changed to its instance. So rather than setting the PipePrefab, set another variable - PipeInstance. Then before destroying PipeInstance, do a null check so that it doesn’t throw errors while PipeInstance is not set yet.

Also, checking PipePrefab’s transform against the deadzone won’t work - the prefab doesn’t exist in the scene. You need to check the transform of PipeInstance.

Pooling is only necessary when you’re instantiating hundreds or thousands of instances in very short timeframes. It adds a lot of complexity and potential for bugs, otherwise.

1 Like

I think I have it for the most part based on your instructions. I don’t know how to “check the transform of PipeInstance” though. Particularly in terms of what node I should connect to the Transform Get Position node.

You should input the PipeInstance graph variable of type GameObject. The same variable that should be set by Instantiate node’s output in place of PipePrefab.

All your code past Instantiation node should operate with the instance of the object, not its prefab, which is just a template that doesn’t exist in Hierarchy. Prefab is like a blueprint describing some objects information and only exists in Project window. When you drag a prefab into Hierarchy manually, it might appear that its the same thing, but when you enter Play mode, the engine automatically instantiates it so it becomes an instance of the prefab.

So if you reference the Prefab in graph directly, you’re just referencing the template in Project window, not Hierarchy. It doesn’t exist in the scene. Therefore, checking its position can’t work - you need to work with the Instantiated version of the prefab that does exist in the scene.

I see! I had tried that but figured I did something wrong because it didn’t destroy the clone. It looks like the issue is that my desired X destroy location is -45 but the clone never gets past 2. At least in the output that is what it shows, even though it does visually go past 1(it’s always a range between 9 and 1). 9 is where it creates the clone but I’m not sure why it doesn’t show it moving past X values lower than 1?

In my screenshot, I set the DeadZone as 0 to test that it doesn’t work. Between 2 and 8 it works correctly.

It occurred to me that it could be that it starts to keep track of the other clone when it spawns in, rather than keeping track of the clone that reaches -45 on the X?