Change Prefab instantiated as GameObject via c#

Pretty simple I guess but couldn’t find an answer yet.

I have an instantiated Gameobject from c# and I want to exchange the prefab of it without Destroying and re-initializing it again.

fooPrefab is a Sphere

barPrefab is a Cube

GameObject tile_go = Instantiate(fooPrefab, position, Quaternion.identity);

When I just overwrite it via

tile_go = Instantiate(barPrefab, position, Quaternion.identity);

I get 2 instantiated GameObjects. I can’t destroy the former one because then I delete necessary references from that object that I need to keep

Is there a way to keep the Gameobject and just exchange the prefab so the Sphere becomes a cube?

Hope someone can help me out here. Sorry for the bad formatting half of formating is wonky and the other half is not possible to find in the help

Option 1: Instantiate other Prefab

To switch out the Sphere with the Cube, you could do something like

GameObject new_tile_go = Instantiate(barPrefab, position, Quaternion.identity);

Sphere oldScript = tile_go.GetComponent<Sphere>();
Cube newScript = new_tile_go.GetComponent<Cube>();
newScript.someReference = oldScript.someReference; // keep references intact

Destroy(tile_go);
tile_go = new_tile_go;

Option 2: SetActive

Have both objects in your scene, and use GameObject.SetActive to disable and enable them when they are needed.

cube.gameObject.SetActive(true);
sphere.gameObject.SetActive(false);