Prefab not scaling with Lerp

I am building a grid with image inside each cell. I am passing an image prefab to method below to resize it according to grid and keep a reference to it - so I dont have to do this calculation over and over.

Later when I am building the grid I set my prefab’s localscale “letterPiece.transform.localScale = m_letterBoxCellObject.transform.localScale;” this works but if I use Lerp to do animation it doesnt, I get really small scale(have used both Matf.Lerp and Vector3.Lerp). any ideas?

Here is the method:

private GameObject ResizeCell(GameObject prefab)
    {
        GameObject cellObject = new GameObject();
        cellObject.AddComponent<Image>().sprite = prefab.GetComponent<Image>().sprite;

        if (prefab == letterPrefab)
        {
            cellObject.AddComponent<LetterPiece>();
        }
        else
        {
            cellObject.AddComponent<Tile>();
        }

        m_cellSize = prefab.GetComponent<RectTransform>().sizeDelta;

        //get the new cell size -> adjust the size of the cells to fit the size of the grid
        Vector2 newCellSize = new Vector2(gridSize.x / (float)m_currentLevelData.LevelData.GridColumns, gridSize.y / (float)m_currentLevelData.LevelData.GridRows);

        //Get the scales so you can scale the cells and change their size to fit the grid
        m_cellScale.x = newCellSize.x / m_cellSize.x;
        m_cellScale.y = newCellSize.y / m_cellSize.y;

        m_cellSize = newCellSize;

        cellObject.transform.localScale = new Vector2(m_cellScale.x, m_cellScale.y);

        //fix the cells to the grid by getting the half of the grid and cells add and minus experiment
        m_gridOffset.x = -(gridSize.x / 2) + m_cellSize.x / 2;
        m_gridOffset.y = -(gridSize.y / 2) + m_cellSize.y / 2;

        return cellObject;
    }

What did your Lerp code look like?

@StarManta

Extension class:

public static class LerpHelpExtensions
{
    public static Coroutine LerpCoroutine(this GameObject gameObj, float time, System.Action<float> block, bool includeZero = false)
    {
        MonoBehaviour behaviour = gameObj.GetComponent<MonoBehaviour>();
        if (behaviour == null)
            return null;

        return behaviour.LerpCoroutine(time, block, includeZero);
    }


    public static Coroutine LerpCoroutine(this MonoBehaviour behaviour, float time, System.Action<float> block, bool includeZero = false)
    {
        return behaviour.StartCoroutine(_LerpCoroutine(time, block, includeZero));
    }

    static IEnumerator _LerpCoroutine(float time, System.Action<float> block, bool includeZero = false)
    {
        if (time <= 0f)
        {
            block(1f);
            yield break;
        }

        float timer = 0f;
        if (includeZero)
        {
            block(0f);
            yield return null;
        }

        while (timer < time)
        {
            timer += Time.deltaTime;
            block(Mathf.Lerp(0f, 1f, timer / time));
            yield return null;
        }
    }

    public static Coroutine AnimateComponent<T>(this MonoBehaviour behaviour, float time, System.Action<T, float> block) where T : Component
    {
        if (block == null)
            return null;

        T component = behaviour.GetComponent<T>();
        if (component == null || !behaviour.gameObject.activeInHierarchy)
            return null;

        return behaviour.StartCoroutine(_LerpCoroutine(time, (timer) =>
        {
            block(component, timer);
        }));

    }
}

Then calling it :

letterPiece.AnimateComponent<Transform>(0.5f, (t, time) =>
       {
        
           Debug.Log("x : " + m_letterBoxCellObject.transform.localScale.x);
           Debug.Log("y : " + m_letterBoxCellObject.transform.localScale.y);
           t.transform.localScale = Vector3.Lerp(testSize, endSizeTest, time);
       });

One minor fix to _LerpCoroutine is that you should call block(1f) at the end of it.

I note that you’re Debug.Logging m_letterBoxCellObject.transform.localScale, but you’re not logging testSize or endSizeTest. Depending on the context in which that code is called, it could be that those values are getting “locked in” to an invalid value.

@StarManta

So I called block(1f); at end of function without if statements…

before doing the animate I set :

testSize = letterPiece.transform.localScale;
endSizeTest = m_letterBoxCellObject.transform.localScale;

Logging them prints following :

x : 0.005509642
y : 0.005509642
testSize : (0.0, 0.0, 1.0)
endSizeTest : (0.0, 0.0, 0.0)

while when doing “letterPiece.transform.localScale = m_letterBoxCellObject.transform.localScale;” I can see the scale in unity inspector being 1.08469 for x and y

@StarManta
Somthing I observed just now is that if I make the AnimateComponent time to 0f it gets the correct localscale.

                testSize = letterPiece.transform.localScale;
                endSizeTest = m_letterBoxCellObject.transform.localScale;
                letterPiece.GetComponent<LetterPiece>().AnimateComponent<Transform>(0f, (t, time) =>
                {

                    Debug.Log("x : " + m_letterBoxCellObject.transform.localScale.x);
                    Debug.Log("y : " + m_letterBoxCellObject.transform.localScale.y);
                    Debug.Log("testSize : " + testSize);
                    Debug.Log("endSizeTest : " + endSizeTest);
                    t.transform.localScale = Vector3.Lerp(testSize, endSizeTest, time);
                });

                Debug.Log("test");
                letterPiece.transform.SetParent(transform);

just a quick heads up it is working now.
so setting the localscale to : letterPiece.transform.localScale = m_letterBoxCellObject.transform.localScale;
then in animatecomponent instead of localscale I resize the sizeDelta :

 letterPiece.AnimateComponent<Transform>(0.5f, (t, time) =>
       {
           t.GetComponent<RectTransform>().sizeDelta = Vector3.Lerp(Vector3.zero, letterPrefabSize, time);
       });

One question, right now all images in grid appear at same time how do I delay it so it appear one by one, I have already tried to put the AnimateComponent in Coroutine but doesn’t work?