1

I am working on a store page. The page is filled with panel prefabs. For some reason when it fills the prefabs it is more than doubling the scale size of the prefab. If I drag the prefab into place it works as expected.

I don't have any code in the program targeting scale. I am not sure where it is coming from.

This is the code I am using for the list population.

[System.Serializable]
public class Item
{
    public string itemName;
    public int price;
}

public class ShopScrollList : MonoBehaviour {
    public List<Item> itemList;
    public Transform contentPanel;
    public Text storeDisplayText;
    public SimpleObjectPool buttonObjectPool;

    void Start () {
        RefreshDisplay();
    }


    private void RefreshDisplay()
    {
        AddButtons();
    }

    private void AddButtons()
    {
        for (int i = 0; i < itemList.Count; i++)
        {
            Item item = itemList[i];
            GameObject newButton = buttonObjectPool.GetObject();
            newButton.transform.SetParent(contentPanel);

            ButtonDetails buttonDetails = newButton.GetComponent<ButtonDetails>();
            buttonDetails.Setup(item, this);
        }
    }
}

SimpleObjectPool Script:

// A very simple object pooling class
public class SimpleObjectPool : MonoBehaviour
{
    // the prefab that this object pool returns instances of
    public GameObject prefab;
    // collection of currently inactive instances of the prefab
    private Stack<GameObject> inactiveInstances = new Stack<GameObject>();

    // Returns an instance of the prefab
    public GameObject GetObject()
    {
        GameObject spawnedGameObject;

        // if there is an inactive instance of the prefab ready to return, return that
        if (inactiveInstances.Count > 0)
        {
            // remove the instance from teh collection of inactive instances
            spawnedGameObject = inactiveInstances.Pop();
        }
        // otherwise, create a new instance
        else
        {
            spawnedGameObject = (GameObject)GameObject.Instantiate(prefab);

            // add the PooledObject component to the prefab so we know it came from this pool
            PooledObject pooledObject = spawnedGameObject.AddComponent<PooledObject>();
            pooledObject.pool = this;
        }

        // put the instance in the root of the scene and enable it
        spawnedGameObject.transform.SetParent(null);
        spawnedGameObject.SetActive(true);

        // return a reference to the instance
        return spawnedGameObject;
    }

    // Return an instance of the prefab to the pool
    public void ReturnObject(GameObject toReturn)
    {
        PooledObject pooledObject = toReturn.GetComponent<PooledObject>();

        // if the instance came from this pool, return it to the pool
        if (pooledObject != null && pooledObject.pool == this)
        {
            // make the instance a child of this and disable it
            toReturn.transform.SetParent(transform);
            toReturn.SetActive(false);

            // add the instance to the collection of inactive instances
            inactiveInstances.Push(toReturn);
        }
        // otherwise, just destroy it
        else
        {
            Debug.LogWarning(toReturn.name + " was returned to a pool it wasn't spawned from! Destroying.");
            Destroy(toReturn);
        }
    }
}

// a component that simply identifies the pool that a GameObject came from
public class PooledObject : MonoBehaviour
{
    public SimpleObjectPool pool;
}

How can I debug to figure out where the issue is coming from. There is only one thing in here affecting the transform and that shouldn't adjust the scale.

12
  • Almost impossible to help without the SimpleObjectPool script. Commented Nov 15, 2016 at 22:57
  • I have added the SimpleObjectPool script Commented Nov 15, 2016 at 23:01
  • Ok. Replace spawnedGameObject = (GameObject)GameObject.Instantiate(prefab); with spawnedGameObject = (GameObject)Instantiate(prefab);. Let me know if the problem is still there. Commented Nov 15, 2016 at 23:08
  • That did not fix the problem Commented Nov 15, 2016 at 23:12
  • How about spawnedGameObject = (GameObject)Instantiate(prefab, prefab.transform.position, prefab.transform.rotation); spawnedGameObject.transform.localScale = prefab.transform.localScale; ? Oops. I forgot the scale too. Take a look at the edit. Commented Nov 15, 2016 at 23:15

1 Answer 1

1

The answer is:

newButton.transform.SetParent(contentPanel, false);

https://docs.unity3d.com/ScriptReference/Transform.SetParent.html

//this makes the player keep its local orientation rather than its global orientation. player.transform.SetParent(newParent, false);

Sign up to request clarification or add additional context in comments.

1 Comment

I actually have seen this problem before and this was the solution. Just saw you comment about newButton.transform.SetParent(contentPanel); now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.