Question by 
               corzokidmcv · Dec 25, 2018 at 05:28 PM · 
                scripting problemscripting beginnerscriptingbasicsscriptableobject  
              
 
              How can I temporary increase the size of an object?
Hello, so I am new to unity and I'm just messing around with code to try and learn. I have a game object that and I would like to increase it's size for example 5 seconds and return to the default size again.
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player : MonoBehaviour {
     public float speed = 5f;
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
 
         if (Input.GetKeyDown("s"))
             transform.localScale = new Vector3(5, 5, 5);
             
 
 
 
 
     }
 }
 
and after 5 seconds i would like it to return to scale of 1,1,1 How would I do this? I'm guessing i use time.deltatime?
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by Hellium · Dec 25, 2018 at 06:46 PM
  using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class Player : MonoBehaviour
  {
      public float growDuration = 5;
      private float growTime;
      private bool grown ;
      
      void Update ()
      {
          if( Input.GetKeyDown( KeyCode.S ) )
          {
              transform.localScale = Vector3.one * 5 ;
              growTime = Time.time;
              grown = true;
          }
          
          if( grown && Time.time - growTime > growDuration )
          {
              transform.localScale = Vector3.one ;
              grown = false;
          }
      }
  }
Answer by Vega4Life · Dec 25, 2018 at 06:44 PM
Here is an example using a coroutine
     using UnityEngine;
     using System.Collections;
 
 
     public class SizeTimer : MonoBehaviour
     {
         [SerializeField] float scaleResetTime = 5f;
         bool isScaled;
 
 
         void Update()
         {
             if (isScaled == false && Input.GetKeyDown(KeyCode.S))
             {
                 transform.localScale = new Vector3(5f, 5f, 5f);
                 StartCoroutine(ResetTime(scaleResetTime));
             }
         }
 
 
         IEnumerator ResetTime(float wait)
         {
             isScaled = true;
 
             yield return new WaitForSeconds(wait);
 
             transform.localScale = Vector3.one;
             isScaled = false;
         }
     }
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                