How would I change my gameobject in game

Right now I’m using this script for switching weapons:0

 public class ShowWeapon : MonoBehaviour

{
    [SerializeField]
    public GameObject item1;
    [SerializeField]
    public GameObject item2;

    public bool ShowItem1;

    public bool ShowItem2;

     void Start()
    {
        ShowItem1 = false;
        ShowItem2 = false;

    }
     void Update()
    {
        if (ShowItem1 == false) { item1.SetActive(false); }
        if (ShowItem1 == true) { item1.SetActive(true); }
        if (ShowItem2 == false) { item2.SetActive(false); }
        if (ShowItem2 == true) { item2.SetActive(true); }
        if (Input.GetKeyDown(KeyCode.Alpha1) && ShowItem1 == false) { ShowItem1 = true; ShowItem2 = false; }
        if (Input.GetKeyDown(KeyCode.Alpha2) && ShowItem2 == false) { ShowItem2 = true; ShowItem1 = false; }
        if (Input.GetKeyDown(KeyCode.R))
        {
            ShowItem1 = false; ShowItem2 = false;
        }
    }
}

and I set my desired gameobject through unity. But I want something more of a pickup system How would I go about doing this without changing this method? (More of a pickup that sets the gameobject)

First of all, consider your code:


if (ShowItem1 == false) { item1.SetActive(false); }
if (ShowItem1 == true) { item1.SetActive(true); }
if (ShowItem2 == false) { item2.SetActive(false); }
if (ShowItem2 == true) { item2.SetActive(true); }

In a very first step, this could be simplified to the more readable


 if (ShowItem1) { item1.SetActive(true); }
 else { item1.SetActive(false); }
 
 if (ShowItem2) { item2.SetActive(true); }
 else { item2.SetActive(false); }

See, you don’t need to use “== false/ true” for bools, you just use “!showItem” (meaning not) or “showItem” (meaning true). It is then also good readability practice to prioritize the positive (instead of the negation), just as you would say in spoken language “When wanting to show the item, activate it” (instead of the longer “When wanting to not show the item, then deactivate it”).


But now we can see you don’t even need the if-else, you can just assign the bool like so:


item1.SetActive(ShowItem1);
item2.SetActive(ShowItem2);

That’s much leaner already! But wait, why are we using numbers in variables? That’s often a good sign you should use an array or a list instead. So we do:


GameObject[] items = new GameObject[2] {item1, item2};

But it’s even easier to let you assign them in the Inspector pane yourself! Just do:


[SerializeField] GameObject[] items = null;

After filling the list, how do you identify which item is active? You can consider using an index int like “activeItem = 1” to point to it (an UpdateItemsVisibility() function could loop through all list items and set only the active one active). Or you could check if the item is “.activeSelf” (a bool that is true when the gameObject is active).


Or, even better perhaps, you could create your own class, “Holdable”, which then handles items to be picked up, and can be attached to items in your scene. Then your main character simply has a “Hand” object or similar, and when it finds another gameObject of the GetComponent<Holdable>() type, it can pick it up by just parenting it to the hand. Whatever is active and held is determined by what is in the hand. And if it’s coming from the inventory, that could be another distinct class attached somewhere to the player.


Good luck!