Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Instantiating new object lots of time, and object ends up being bigger

Discussion in 'Scripting' started by TheCadFry, Feb 23, 2023.

  1. TheCadFry


    Jan 17, 2021
    Okay hello! One of my first posts here so I am sorry if it doesn't make sense or if this is the wrong spot. Essentially I have this inventory system that drops your item on right click. It does, quite well in fact, but for some reason after doing it a few times, the object kept getting bigger. Unsure why any help would be appreciated! Attached you can find the code and a picture of what I mean!

    public void DropItem()
    if (amountInStack == 0)
    if (amountInStack == 1) {
    item.GetComponent<Item>().pickedUp = false;
    // Gathers information about player position, and drops the item in front of the person 3 units
    Vector3 playerPos = player.transform.position;
    Vector3 playerDirection = player.transform.forward;
    Quaternion playerRotation = player.transform.rotation;
    float spawnDistance = 3;
    Vector3 spawnPos = playerPos + playerDirection * spawnDistance;
    Instantiate(item, spawnPos, playerRotation);
    item.GetComponent<Item>().pickedUp = false;

    // Resets variables on the slot back to default(ish)
    empty = true;
    ID = 0;
    type = null;
    item = null;
    description = null;
    theItemName = null;
    icon = null;
    slotIconGO.GetComponent<Image>().sprite = null;
    GetComponentInChildren<TMP_Text>().text = null;
    GetComponentInChildren<TMP_Text>().text = amountInStack.ToString();

  2. TzuriTeshuba


    Aug 6, 2019
    I cant say what cause the object to get larger each time, but i have a hunch it is somewhat related to this line:
    Code (CSharp):
    1. Instantiate(item, spawnPos, playerRotation);
    you probably want it to be something like:
    Code (CSharp):
    1. ItemObject instanceOfItem = Instantiate(item, spawnPos, playerRotation);
    in your code, you create an instance of the item object but dont capture a reference to what you just created. the "item" you pass into the Instantiate method is the prefab, not the instance of the prefab you just instantiated. so if at some point you scale up the prefab instead of the instance, that scaling will be reflected in future instantiations of the item object.

    Also, please use code tags in the future. i see you gave sort of formatting, but code tags give color coding to keywords and c# syntax which is easily the difference between someone helping you here and just clicking away.
    chemicalcrux likes this.