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\$
  • \$\begingroup\$ Have you considered using DoTween? It is imo the best tweening lib for unity. \$\endgroup\$ – Sidar Aug 21 '17 at 10:15
  • \$\begingroup\$ @Sidar I'll try that, thx for the info \$\endgroup\$ – Edwin Harly Aug 23 '17 at 2:20
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\$
  • \$\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\$ – Edwin Harly Aug 23 '17 at 2:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.