- Home /
How to temporarily resize a GameObject for a few seconds then revert it back ?
This is my current code
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;
}
}
So far, the player GameObject are getting bigger, but it didn't revert back, also the color change into black when the size change and it should not be.
Answer by edwinharly · Aug 22, 2017 at 01:57 AM
So I've solved this problem, instead of storing the original scale to a variable then to apply it back to the game object after a few seconds, I try a more static approach, which is I set the scale to a new Vector3 with the original scale values. I know this isn't the best solution for everyone, but this works for me now.
Here is the final code
private IEnumerator applyPowerUp(string type, GameObject player)
{
// GameObject originPlayer = player;
Vector3 originalScale = player.transform.localScale;
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, player.transform.position.y);
yield return new WaitForSeconds(5f);
Debug.Log("after 5 secs");
player.transform.localScale = new Vector3(0.5f, 1.7f, 1f);
}
}
Answer by Cherno · Aug 21, 2017 at 09:35 AM
first of all, these lines:
GameObject originPlayer = new GameObject();
originPlayer = player;
mean that you create a new empty GameObject, and then assign an existing GO to the originPlayer variable so you lose the reference to the new GO and thus have an empty GO in your scene that you don't have any reference too. I'm pretty sure you don't want that.
Instead, do it like this:
GameObject originPlayer = player;
Now you have a reference to the player GO.
However, then you change the scale of the player GO and later assign the originPlayer GO to the player variable, which both point to the same GO, so there's a logical flaw.
Instead, just store the original scale of the player GO and use it to reset the scale of the player GO.
private IEnumerator applyPowerUp(string type, GameObject player)
{
Vector3 scale_original = player.transform.localScale;
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.transform.localScale = scale_original;
}
}
Thanks for the reply mate, but it doesn't work, the player GameObject still didn't revert back
Your answer
Follow this Question
Related Questions
Coroutine - transform for every frame for duration? 0 Answers
Moving multiple transforms from an array in a single script 0 Answers
Coroutines WaitForSeconds – uneven spacing 2 Answers
Why isn't this coroutine fully firing? 1 Answer
Looping a Script 2 Answers