### 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)

Ref<InputEventScreenTouch> st = ev;
		if (st.is_valid()) {
			pos = st->get_position();
		}
		if (ss2d) {
			
			uint64_t frame = get_tree()->get_frame();
			PhysicsDirectSpaceState2D::ShapeResult res[64];
			for (const CanvasLayer *E : canvas_layers) {
				Transform2D canvas_layer_transform;
				ObjectID canvas_layer_id;
				if (E) {
					
					canvas_layer_transform = E->get_final_transform();
					canvas_layer_id = E->get_instance_id();
				} else {
					
					canvas_layer_transform = get_canvas_transform();
					canvas_layer_id = ObjectID();
				}
				Vector2 point = canvas_layer_transform.affine_inverse().xform(pos);
				PhysicsDirectSpaceState2D::PointParameters point_params;
				point_params.position = point;
				point_params.canvas_instance_id = canvas_layer_id;
				point_params.collide_with_areas = true;
				point_params.pick_point = true;
				int rc = ss2d->intersect_point(point_params, res, 64);
				if (physics_object_picking_sort) {
					struct ComparatorCollisionObjects {
						bool operator()(const PhysicsDirectSpaceState2D::ShapeResult &p_a, const PhysicsDirectSpaceState2D::ShapeResult &p_b) const {