How do I scale a instantiate prefab before I spawn it?

I have a code that allows me to spawn objects using instantiation, I can simply mouse down and I would get the object to follow my cursor and then once I release (mouse up) the object drops and fully spawns along side its properties get activated (e.g. rigidbody).
I’m trying to use 3 sliders and work with the code I have but just can’t figure out how to make it work with instance of an object and can only scale objects that have already spawned, once again I don’t want the all the prefab clones (instances to grow) just the ones that currently being spawned as I click.

if(Input.GetMouseButtonUp(0))
            {
          
                Vector3 spawnPos = Spawner.transform.position;
                Destroy(Spawner);
                Spawner=Instantiate(prefab,new Vector3(spawnPos.x,spawnPos.y,spawnPos.z), Quaternion.identity) as GameObject;

                //Destroy (GameObject.FindWithTag("DestroyobjTemp"));
                Spawner.GetComponent<Rigidbody>().isKinematic = false;
                Spawner.GetComponent<Collider>().enabled=true;
                Spawner.GetComponent<Collider>().isTrigger=false;
                Spawner.layer = LayerMask.NameToLayer("Default");
                Spawner= null;

}

Spawner.transform.localScale = new Vector3(x, y, z);

unless I’m not understanding what you’re trying to do? You don’t want to scale the prefab before you instantiate - just instantiate and then scale. It all happens in the same frame so the user won’t know the difference.

1 Like

I’m not sure sure however it works better, as long as each clone has its unique scaling factors upon slider property changing. but then the original is changing so I’m not sure what effect that has on the clones.
I have a script attached to my camera with a game object variable which I’ve dragged my prefab to it within my inspector.
Also how do I make it work with a slider, can you assist me with this please? where do I put my void before mouse up parameter or after, also do I need to assign a gameobject on my parameter?

I just did

public void setobjScaling (){
        if (Input.GetKey (KeyCode.P))

            Spawner.transform.localScale = new Vector3(10, 10, 10);


     
     


    }

as a test it didn’t even work
is there anything wrong with this or anything I need to assign?
Also what would be the code to use for the the slider? one last thing where section do I stick this code before the mouse down after? or inside my mouse up if statement? - thanks.

  • update: got it to work now I just need to work out how to use the slider, many thanks if you could spare some of your time to help me with this.

One last thing kind sir, how do I set a limit to scaling? e.g. I don’t want the x y and z value to go over 5.
#- update: got it to work now I just need to work out how to use the slider, many thanks if you could spare some of your time to help me with this.

Ok - so ten million questions at once… :slight_smile:

  • Scale it when you instantiate it
  • Depends on what type of slider you’re using
  • Don’t clamp the value, just make the max value of the slider 5

Thanks a lot friend, also how would I assign the code to the slider? do I need to call for the slider within my code? or call and assign the function within the slider itself in the inspector?

Read the value of the slider when you’re ready to scale the thing

What do you mean the value? I’m sorry if I sound stupid just confused, I did just set the value to 5 on the inspector property for the slider.

You have to get a reference to the slider you want and then get the value of that slider.
https://docs.unity3d.com/ScriptReference/UI.Slider.html