Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Prefab Drag-in, randomize scale.

Discussion in 'Editor & General Support' started by crazycrinkle, May 28, 2018.

  1. crazycrinkle

    crazycrinkle

    Joined:
    Jan 4, 2014
    Posts:
    26
    Hi there, I am wondering if this is possible, as otherwise i will have to manually adjust hundreds of meshes one by one. I am trying to know if it is possible to attach a script to a prefab. Which runs on import (when the prefab is dragged onto the 3d scene). Basically I wan't each instance of the prefab to have random scales in x,y,and z. Is this possible with scripts, or any other way to randomize the prefab instances quickly?
     
  2. WoodcockCreative

    WoodcockCreative

    Joined:
    Feb 16, 2017
    Posts:
    45
    Ok, this is a bit of a hack, but it will get you what you want. Drop a script onto the object you want to rescale with the following code:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class AScript : MonoBehaviour
    4. {
    5.  
    6.     void Start()
    7.     {
    8.         Vector3 randScale = new Vector3(Random.Range(1.0f, 2.0f), Random.Range(1.0f, 2.0f), Random.Range(1.0f, 2.0f));
    9.         transform.localScale = randScale;
    10.     }
    11. }
    12.  
    Then drag into the scene as many of the objects you want. Run.
    Next, while running select all the rescaled objects and copy them.
    Then stop the scene, delete the rescaled objects and repaste them from your clipboard with the random scale.
    Then comment out the scaling code or remove the scripts from them all (can be done in one step).
     
  3. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    Here you go: a simple scale randomizer script. Just drop it into an Editor folder in your project. It will create a new menu item at
    Tools > Scale Randomizer
    . It operates on selected transforms, so just select all the objects you want to transform, configure it how you'd like, and press the button!

    https://gist.github.com/Madgvox/a1b5aaa28d27557fceb7216ebe464bd3
     
    ToastyJ likes this.
  4. crazycrinkle

    crazycrinkle

    Joined:
    Jan 4, 2014
    Posts:
    26
    Thats suspiciously easy! Thanks!
     
  5. ToastyJ

    ToastyJ

    Joined:
    Oct 8, 2020
    Posts:
    7
    Super cool! Thanks very much. This should be included as baseline install >_<.
    If you ever get a chance, a random rotation would be a great addition to this tool.
     
  6. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
  7. valba

    valba

    Joined:
    Nov 11, 2017
    Posts:
    5
    @Madgvox Thanks for the script, I was looking for something like this and your code is perfect. Well, almost ;), there is a small typo in line 395, it says
    Code (CSharp):
    1. minRotation.x
    and I think it should say
    Code (CSharp):
    1. minPosition.x
    Again, thank you for sharing this code.
     
    Madgvox likes this.
  8. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,315
    Glad it was helpful to you. :)

    Fixed!