Animate the scaling of a gameObject

How can I animate the scaling of a gameObject over time? Basically, when the image gets collided into, I want it to scale bigger temporarily, and then back to its original size. I’m using the following code to make it scale bigger, which works:

snareImage= GameObject.FindWithTag("snareImage");
snareImage.transform.localScale = new Vector3(1.1f, 1.1f, 1.1f);

But how can I animate this and scale back to it’s original size?

Thanks!

I just want to add this answer for non-coders and maybe an even better way of animating the scaling of an object. This can be done using Animation tool Unity provides.

  1. First, Go to to Window → Animation so you are viewing the animation tab.
  2. Highlight the GameObject in the Scene or any Prefab you want to create an animation for.
  3. A Create Animation button will appear in the Animation tab. Click it.
  4. Now you are prompt to create an animation. On the left corner of it there should be a red dot or record button. Click on it.
  5. Now that you are recording anything you do will be recorded. Now drag the RED line to a preferred time.
  6. Scale the gameobject under the INSPECTOR tab to the preferred scale.
  7. You will now see Keys be created under the RED line. Congratulations you have now created your animation!
  8. Now all you have to do is play the animation when the necessary through code or you can add to your Created Animation to your controller and add parameters to play the animation when necessary!

I know this can be hard to follow for some people. So here I will provide a video that closely demonstrates of what I am talking about: 6. Unity tutorial: Scrolling combat text - Critical strikes - YouTube

Use Vector3.Lerp; it’s easiest if you use a coroutine. You can work off of this; it’s basically the same thing as the Translation function except you’d use transform.localScale instead of transform.position.

You have to put it inside Update() function
Update() function runs every frame so in each frame you object will scale a delta-scale amount.

See the docs for how Update() function works

yes i know this is an old post but i was tring to do the same i came up with the following script, but im a noob really so this can be expanded upon using lerp and such but i didnt need it hope somebody finds a use for this

   using UnityEngine;
   using System.Collections;

  public class bounce : MonoBehaviour {

public AudioClip bounceNoise;
public AudioSource source;        //Set in Inspector 
public float speed;                           //Set in Inspector 
public GameObject bouncer;              //Set in Inspector 
public float explosionStrength = 100;    //Set in Inspector 

void Awake() {
    source = GetComponent<AudioSource>();
}
void Update()
{
    
}
IEnumerator MyCoroutine()
{
    scale();
    yield return new WaitForSeconds(speed);
    normalFlash();
}
void OnCollisionEnter(Collision _other)
{
    _other.rigidbody.AddExplosionForce(explosionStrength, this.transform.position, 5);
    source.PlayOneShot(bounceNoise, 1f);
    StartCoroutine(MyCoroutine());
}
void scale()
{
    bouncer.transform.localScale = new Vector3(1.2f, 1.2f, 1.2f);
}
void normalFlash()
{
    bouncer.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
}
}

`