Creating separate clones of a referenced prefab (might need a whole different approach)

Okay, all this exists within the context of a card game, where your cards are determined by your worn equipment/weapons. I have the issue below with both how Im trying to work with my class and class, both having bad interactions in different ways.

DESIRED OUTCOME: I use resources.load (I know this is slow, thats fine) to create a reference to a prefab which includes with has all the variables set how I want for that object. [code snippet #1 below, using equipment]. Then, when I need to show that object to the player I instantiate a clone of it [code snippet 2 below, + bringing up the inventory screen] . With Equipment specifically, I want to be able to “dual wield” weapons, which means I might have 2 weapons that are the same. (or two fists for unarmed in the snippets) THEN, when I equip a new item (say a shortsword) snippet 2 removes all cards associated with the item that was there before.

THE ISSUE WITH : When changeItem() (snippet 2) checks to see if the cards source item (which is declared before adding the cards to the deck) it cannot differentiate between the 2 equipments if they have the same base prefab. SO in this scenario, both unarmed fist slots add 5 cards for a total of 10. Then I equip a sword which should remove the 5 cards associated with whichever hand I equipped it to, and replace them with 5 sword cards. But instead, it reads every card as from the fist being “removed” and gets rid of them all which leaves me with the 5 sword cards.

THE ISSUE WITH : Im pretty sure if I can figure out how to solve the issue with the equipment, I can solve the card issue too but here it is, Cards can be dynamically changed both during combat or in the outside overworld. But with this method of reference, If I change one card’s variable, it changes all other cards with the same base prefab. aka, if one “Punch” card gets its cost lowered to 0, every other punch card does as well. I have a workaround with this for right now, but its very ugly and not worth sharing. Please focus help on the equipment.

I do not think any other code is relevant here, but if you disagree lmk ill share all I can. Ty for any help or advice.

SNIPPET #1, in character, called if a character has nothing equipped in start()
** **GameObject toLoadMainHand = (GameObject)Resources.Load("Equipment/Fist");** **GameObject toLoadOffHand = (GameObject)Resources.Load("Equipment/Fist");** **changeItem(toLoadMainHand.GetComponent<Equipment>(), 1);** **changeItem(toLoadOffHand.GetComponent<Equipment>(), 2);** **updateInventory();** **
SNIPPET #2, also in character, called above
```csharp
**public void changeItem(Equipment item2Eqip, int slotType, bool leaveEmpty = false)
{
inventory.Add(equippedItems[slotType]); //adds to inventory before removing equipped

    List<Card> toBeRemoved = new List<Card>();
    foreach (Card r in deckMain.thisDeck)
    {
        if (r.sourceItem == equippedItems[slotType])
        {
            toBeRemoved.Add(r);
            Debug.Log("scrubbed  cards from main");
        }
    }
    foreach (Card R in toBeRemoved)
    {
        deckMain.removeCard(R);
    }
   //code which adds the new items cards is here, which is working fine so not included

}

public void updateInventory()
{                           //this is used to update the inventory screen
    if (SP == null)
    {
        SP = FindObjectOfType<SceneParent>();    // I think this is a "blackboard", if that helps
    }
    if (SP.activeCharacter == this)
    {
        Destroy(GameObject.FindWithTag("ISMainHand").transform.GetChild(0).gameObject);
        Instantiate(equippedItems[1].gameObject, GameObject.FindWithTag("ISMainHand").transform);
        Destroy(GameObject.FindWithTag("ISOffHand").transform.GetChild(0).gameObject);
        Instantiate(equippedItems[2].gameObject, GameObject.FindWithTag("ISOffHand").transform);

}**
```

Hi, try and use PrefabUtility.InstantiatePrefab(…) instead of Resources.Load. That way they the other don’t change when you modify the value.
This also solves the problem of not being able to distinguish them as they will now be separate GameObject while still tied to the asset.

My solution, which worked as I wanted it to, was this

GameObject toLoadMainHand = (GameObject)PrefabUtility.InstantiatePrefab(Resources.Load("Equipment/Fist"));
GameObject toLoadOffHand = (GameObject)PrefabUtility.InstantiatePrefab(Resources.Load("Equipment/Fist"));
PrefabUtility.UnpackPrefabInstance(toLoadOffHand, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction);
PrefabUtility.UnpackPrefabInstance(toLoadMainHand, PrefabUnpackMode.Completely, InteractionMode.AutomatedAction);
changeItem(toLoadMainHand.GetComponent<Equipment>(), 1);
changeItem(toLoadOffHand.GetComponent<Equipment>(), 2);

However I just learned that you cannot use PrefabUtility in a build of a game, only in editor. So I’m not sure where to go from here but ty for the time

I guess that means my whole approach is flawed/ not supported. Because what I WANT to do it not load items until the first time they are referenced, instantiate them, save a copy of them in a list (inventory) and then destroy the game object, and just instantiate them later from the list. but they are not a copy of the gameobject being stored in a list, they are a reference to the first gameobject, so destroying the original breaks the reference. (I realize now I did not mention any of this in the OP)

SO, would that make the proper thing to do to just instantiate items when they are referenced the first time, and just place them on some sort of “Dump” empty gameobject, and then instantiate off of that as needed? That seems clunky to me, but none of these items have any sort of update() or fixedUpdate(). Or maybe I just dont know the alternate method of creating a separate, unique copy of a prefab which can be altered later, without actually instantiating a gameobject into the scene.

oh my god. Ive been entirely overthinking this havent I? I CAN have a itemDump gameobject but all it needs is a list and I can add each prefab to that list, right? It might get a little tedious doing that by hand eventually but then i just HAVE the gameobjects in a nice list without having to instantiate them all. I’m almost mad about how simple that is after struggling for the last 2 days.

Side note with the cards, since 90% of the cards come from equipment, I can apply the same logic to those cards. Each equipment already has a list of associated cards, I can just apply them in the inspector to the equipment prefab. Again, tedious but exactly what I want.