Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by edwinharly · Aug 21, 2017 at 09:19 AM · transformcoroutinewaitforseconds

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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); 
         }
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
          }
      }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image edwinharly · Aug 22, 2017 at 01:53 AM 0
Share

Thanks for the reply mate, but it doesn't work, the player GameObject still didn't revert back

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

84 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges