how do I change one object for another when the object is clicked?

here is the script i am using at the minute to change the material of a sphere so it changes colour when the mouse is over it, instead what i am trying to do is change the object from a sphere to a building when the sphere is clicked, i did the colour thing to make sure the script worked and now i cant figure out how to change the sphere to a building instead, the idea behind this is the user/player is clicking on a push pin to show the building connected with its position on a globe.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Selectionmanager : MonoBehaviour
{
    [SerializeField] private string selectableTag = "Selectable";
    [SerializeField] private Material highlightMaterial;
    [SerializeField] private Material defaultMaterial;

    private Transform _selection;

    private void Update()
    {

        if (_selection != null)
        {
            var selectionRenderer = _selection.GetComponent<Renderer>();
            selectionRenderer.material = defaultMaterial;
            _selection = null;

        }

        var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out RaycastHit hit))
        {

            var selection = hit.transform;
            var selectionRenderer = selection.GetComponent<Renderer>();
            if (selectionRenderer != null)
            {
                selectionRenderer.material = highlightMaterial;
            }

            _selection = selection;
        }

    }
}

i’m not crazy bothered by it being well optimised at this point i just want something functional.

thank you in advance!

Generally if the script is working fine to update the Material of the object itself, then all that you would have to do is:

  1. Set the Building position (transform.position) to the one of the Selected Sphere
  2. hide the Sphere (assuming you want to reuse it later possibly)

All you need for that is a reference to the Sphere GameObject and the Building GameObject.

In case of your script you already have access to the Sphere (or whatever else you select) under _selection

So all you need is a Building GameObject. You could Instantiate one with Unity - Scripting API: Object.Instantiate assuming that your building is a prefab or simply already have one in your scene, but disabled.

Only “issue” is that your script doesn’t check for anything on the hit.transform outside of the Renderer… so you could basically select anything with that RayCast, not just a specific sphere. Meaning that you probably want to also check in some way that the selected object actually should be replacable by a building (or anything else, really)

Could probably try to use Tags or simply checking the name of the object (assuming you always name those spheres the same ) or a script on the sphere itself that you check for the same way you do for the rendered (gameObject.GetComponent() != null)

Hope this helps :slight_smile: