how to properly scale gameobject in script

I’m trying to make a gun that the bullets growing in size while you hold down the fire key i got it working by just creating multiple instances of the object but i cant figure out how to destroy them after the next object is place. is there a better to scale it as to not run into this problem?

heres my code:

using System.Collections;
using UnityEngine;

public class gunfire : MonoBehaviour
{
public Rigidbody projectile;
public Transform chargeSpawn;
public GameObject bubble;

public Transform spawnpoint;
public bool isGrowing = false;

IEnumerator growCoroutine;
void Start()
{
    growCoroutine = growCor();
}

private void Update()
{
    if (Input.GetMouseButtonDown(0))
        growStart();
    if (Input.GetMouseButtonUp(0))
        growStop();
}
IEnumerator growCor()
{
    
    while (isGrowing) {
        Instantiate(bubble, spawnpoint.position, spawnpoint.rotation);
        bubble.transform.localScale += new Vector3(0.025f, 0.025f, 0.025f);
        var radius = transform.localScale.y;
        
        if (radius == 3.7) {
            growStop();
        }
        yield return null;

    }
}

void growStart() {
    if (!isGrowing) {
        isGrowing = true;
        StartCoroutine(growCoroutine);
    }
}

void growStop() {
    if (isGrowing)
    {
        StopCoroutine(growCoroutine);
        isGrowing = false;
        bubble.transform.localScale = new Vector3(1f, 1f, 1f);
        var radius = transform.localScale.y;
        float speed = (100 / radius);
        Rigidbody InstantiateProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
        InstantiateProjectile.velocity = transform.TransformDirection(new Vector3(speed, 0, 0));
    }
}

}

Hi @unity_i0W6HQ-YEHqs4Q, so to clarify, if the player holds the button down, a bubble begins to form on the player’s gun and grows. Then on release, the bubble becomes a projectile. Is that what you intend?

I don’t have a lot of experience with Coroutines, I usually just do operations like this inside class Update functions. Are you using Coroutines for a performance reason?

Either way, I don’t think you’d need to instantiate new objects to accomplish this effect… just create a growingBubble variable to refer to the bubble that is growing, then scale it and move it to the spawnpoint in the while loop… this is assuming the player can move while the bubble is growing so you need to have the bubble follow the gun. I would also recommend using a variable for the grow rate so you can experiment with it in the inspector at runtime. Something like this:

GameObject growingBubble;
float growRate = 0.025f;

IEnumerator growCor()
 {
     growingBubble = Instantiate(bubble, spawnpoint.position, spawnpoint.rotation);
     while (isGrowing) {
         growingBubble.transform.localScale += Vector3.one * growRate;
         growingBubble.transform.position = spawnpoint.position;
         var radius = transform.localScale.y;
         
         if (radius == 3.7) {
             growStop();
         }
         yield return null;
     }
 }

I don’t see how you are currently using the grown bubble to shape your projectile, but with a variable in your class, you could access it inside growStop() how you need to. If you want the bubble to grow smoothly with time, you should also involve Time.deltaTime when you apply the growRate. There’s a number of articles and tutorials out there about how to properly use deltaTime to change objects smoothly with time.

Hope that helps!