How to make an object scale indefinately on key press?

I’m using a code which doesn’t seem to work for me. On Space press, I manage to instantiate the prefab but it does not scale for some reason. Thanks for your time :slight_smile:

using UnityEngine;
using System.Collections;

public class SpawnSmashRadius : MonoBehaviour {

	private GameObject smashRadius;
	float speed = 5f;

	
	void Update ()
	{
		if(Input.GetKey(KeyCode.Space))
		{
			GameObject radius;
			radius = Instantiate(smashRadius, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
			radius.transform.parent = smashRadius.transform;
		}
		//Scale the object
                 Vector3 newScale = gameObject.transform.localScale;
                 gameObject.transform.localScale += gameObject.transform.localScale * Time.deltaTime * speed;
                 gameObject.transform.localScale = newScale;

		

	}
}

It is due to your code negating the changes already made.

Vector3 newScale = gameObject.transform.localScale; // You set the newscale to be the original unmodified scale
gameObject.transform.localScale += gameObject.transform.localScale * Time.deltaTime * speed; // You increase the scale of the object with the Time.deltaTime factor
gameObject.transform.localScale = newScale; // You set the scale to the original ummodified one , effectively negating the change you made to the previous code.

You should remove the last line (gameObject.transform.localScale = newScale;)in this code segment.

Problem #1, you are using Input.GetKey() instead of Input.GetKeyDown(). Even for a brief press, you will get multiple frames with and therefore multiple Instantiated objects. Problem #2, you are scaling the game object with this script, not the game object you just created. And potential problem #3, ‘smashRadius’ is never initialized (at least not in the code you posted).

I’m not sure how you want this to work, but try this:

using UnityEngine;
using System.Collections;

public class SpawnSmashRadius : MonoBehaviour {
	
	private GameObject smashRadius;
	float speed = 5f;
	GameObject radius;

	void Update ()
	{
		if(Input.GetKeyDown(KeyCode.Space))
		{
			radius = Instantiate(smashRadius, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
			//radius.transform.parent = smashRadius.transform;
		}
		//Scale the object
		if (radius != null) {
			Vector3 newScale = radius.transform.localScale;
			newScale += gameObject.transform.localScale * (Time.deltaTime * speed);
			radius.transform.localScale = newScale;
		}
	}
}

And you didn’t need to use the ‘newScale’ temporary variable. You only have to use a temporary if you are trying to change a single component (x,y,z) rather than the whole thing. You could do:

 radius.transform.localScale += gameObject.transform.localScale * (Time.deltaTime * speed);

Bingo ! That is exactly what I needed. Such a simple fix to that ! Thanks a lot for all the help and suggestions. So, here is the final code that worked for me.

void Update (){
		if(Input.GetKeyDown(KeyCode.Space))
		{
			cloneRadius = Instantiate(smashRadius, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
			cloneRadius.transform.parent = smashRadius.transform;
		}

		//Scale the object
		if (Input.GetKey(KeyCode.Space)) {
			if (cloneRadius != null) {

				cloneRadius.transform.localScale += gameObject.transform.localScale * (Time.deltaTime * speed);

			}