0
\$\begingroup\$

I've tried to make a coroutine like this

private IEnumerator applyPowerUp(string type, GameObject player)
{
    GameObject originPlayer = new GameObject();
    originPlayer = player;

    if (type == "Bigger")
    {
        Debug.Log("make it big");
        player.transform.localScale = new Vector3(1.25f, 2.9f, 0);
        player.transform.position = new Vector2(player.transform.position.x + 0.4f, player.transform.position.y);
        yield return new WaitForSeconds(5);
        player = originPlayer;
    }
}

The game object are getting bigger but after 5 secs it didn't turn back to its origin size.

\$\endgroup\$
2
  • \$\begingroup\$ Have you considered using DoTween? It is imo the best tweening lib for unity. \$\endgroup\$
    – Sidar
    Commented Aug 21, 2017 at 10:15
  • \$\begingroup\$ @Sidar I'll try that, thx for the info \$\endgroup\$ Commented Aug 23, 2017 at 2:20

1 Answer 1

2
\$\begingroup\$
GameObject originPlayer = new GameObject();
originPlayer = player;

this doesn't do what you think it does.

instead you want to save the just the original scale and restore that:

if (type == "Bigger")
{
    Vector3 origScale = player.transform.localScale;

    Debug.Log("make it big");
    player.transform.localScale = new Vector3(1.25f, 2.9f, 0);
    player.transform.position = new Vector2(player.transform.position.x + 0.4f, player.transform.position.y);

    yield return new WaitForSeconds(5);

    player.transform.localScale = origScale;
    player.transform.position = new Vector2(player.transform.position.x - 0.4f, player.transform.position.y);
}
\$\endgroup\$
1
  • \$\begingroup\$ I've tried this approach but it didn't solve the problem, the scale still didn't back to the previous one. So, I try to assign the localScale with new Vector2 with the value same as the previous scale and it worked. Thx anyway. \$\endgroup\$ Commented Aug 23, 2017 at 2:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .