### Godot version

v4.0 rc6

### System information

Windows 10

### Issue description

When using a ViewportTexture from a SubViewport on a TextureRect, it throws errors after running the scene once and then switching scenes in the editor.
![Pasted image 20230227212626](https://user-images.githubusercontent.com/44676282/221680047-984fa03e-33d1-4856-9a7f-1700b28423cf.png)


### Steps to reproduce

Run the canvas-scene once.
Switch to another scene in the editor.
Switch back to the canvas-scene

### Minimal reproduction project

[ViewportTextureError.zip](https://github.com/godotengine/godot/files/10844099/ViewportTextureError.zip)

CollisionObject2D *a = Object::cast_to<CollisionObject2D>(p_a.collider);
							CollisionObject2D *b = Object::cast_to<CollisionObject2D>(p_b.collider);
							if (!a || !b) {
								return false;
							}
							int za = a->get_effective_z_index();
							int zb = b->get_effective_z_index();
							if (za != zb) {
								return zb < za;
							}
							return a->is_greater_than(b);
						}
					};
					SortArray<PhysicsDirectSpaceState2D::ShapeResult, ComparatorCollisionObjects> sorter;
					sorter.sort(res, rc);
				}
				for (int i = 0; i < rc; i++) {
					if (res[i].collider_id.is_valid() && res[i].collider) {
						CollisionObject2D *co = Object::cast_to<CollisionObject2D>(res[i].collider);
						if (co && co->can_process()) {
							bool send_event = true;
							if (is_mouse) {
								HashMap<ObjectID, uint64_t>::Iterator F = physics_2d_mouseover.find(res[i].collider_id);
								if (!F) {
									physics_2d_mouseover.insert(res[i].collider_id, frame);
									co->_mouse_enter();
								} else {
									F->value = frame;