- Home /
Instantiated prefab scale is wrong.
Hello, and I hope my first question here hasn't been answered in a million places, I have been looking all day.
I have a project that instantiates a prefab (sol1) through a script using this code.
var sol1 : Transform;
var a1s = Instantiate(sol1, Vector3(0, 0, 0), Quaternion.identity);
This code was directly lifted from another project and works famously, but when used in this project the prefab is instantiated with the wrong scale, usually 0.999999 something on all axis. This does not happen in any of my other projects, where prefab scale is simply carried over into the scene. I don't understand why this happens here. Also, trying to access the scale to change it after it is instantiated has no effect. I have tried instantiating it to the position and rotation of an empty, but it is the same. It seems everything does what it is supposed to, except it refuses to keep the prefab scale and let me access the scale after the object is spawned. I have also tried this with two versions of the prefab, one off the project list and one out of the scene itself, with no difference. Bringing in and using a prefab that worked in a separate project with this code resulted in two out of three axis over-scaling incorrectly with the third scaled down to 1.
I am unbelievably lost in what looks to me like random behavior in Unity and I have no idea what to try next beyond just scrapping it and starting over. Please help!
-Brother B
I found the problem, and I feel like such a moron. The problem is with Blender 2.61, not Unity. I tried all sorts of ways to modify and re-export the objects with no luck. I opened a new Blend file, made a new object from scratch, and exported it into the project. It too had the scaling problem. Finally, I gave up, and as a last ditch attempt I re-opened my original model in Blender 2.59 instead of 2.61 and exported it one last time, this stopped the forced rescaling.
I'm really happy to know I'm not crazy and I would like to say that I LOVE Unity and was perfectly willing to do what it takes to work around this if necessary. Thanks, everyone, for helping out.
-Brother B.
How are you finding that the scale is .999..? Where does it say this?
Can you see any visual difference (0.0001% error)?
It's because the way real numbers are calculated in computers. http://en.wikipedia.org/wiki/Floating_point#Representable_numbers.2C_conversion_and_rounding
The scale of .999999 is shown in the Inspector under scale, where I can see the difference between the original prefab and the Instantiated version. Also, yes, there is a visual in scene difference, that is how I first noticed. What is interesting is that I can drag the prefab out into the scene and it scales fine in accordance with the prefab scale, but when Instantiated it's very tiny by comparison.
It cannot be tiny because of a scale of 0.9999999. Because of floating point precision, there is no difference between 1.0 and 0.9999999.
The prefab is scaled to 10, 10, 10. This is not being maintained when Instantiated.
Answer by Kryptos · Jan 12, 2012 at 10:20 PM
One important thing to know is that any change to the transform of an instance prefab will not be reflected on the original prefab when clicking on "Apply". This can be easily understood: while you want all your instantiated prefabs to share every components and parameters, you may want to have them at different place/size/rotation without breaking prefab connection.
Verify that your prefab (not an instance of this prefab in your scene) scale is indeed (10, 10, 10).
Try to instantiate your prefab using the method with one argument only (although I'm pretty confident that the other one will still work).
I did some test. It worked fine for me.
It is indeed 10, 10, 10, as seen from the Project Tab itself. I created a new script and a new test scene, using the following code attached to the main camera, the only object present.
var thing1 : Transform;
function Update () {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.S)) { var t1 = Instantiate(thing1); }
}
The result is that the scale of the instantiated object is now 0.9999998, 0.9999998, 1 as in the original scene.
It also changes the prefab rotation from 0, 0, 0 to -4.242594e-06, 8.439047e-07, 348.75 (but that's a whole 'nother road of crazy I didn't want to go down yet)!
What I just noticed is that if I drag and drop the prefab from the Project window to the scene, the scale is 10, 10, 10 UNTIL I press Play, then it too rescales to 0.9999998, 0.9999998, 1. This is without pressing the S key or doing anything else once it starts.
There is a difference though in this scene than in my original. I have another prefab with a scale of 30, 30, 30, that if I plug into thing1 in this test scene spawns without rescaling. However, in the original scene it too would be rescaled, only to 100, 100, 1.
I feel like what I am typing is madness and you're all sitting back shaking your heads solemnly at my mental illness! Or worse, looking for hidden cameras.
Upload an (reduced) example of your project somewhere so we can help you.
$$anonymous$$y guess is you have another script rescaling everything (or some object of some type).
I didn't find any camera. But that's the purpose of hidden cameras: they are hidden O_O'
Answer by ematsuno · Mar 22, 2018 at 04:06 PM
I ran into the same situation when I instantiated a prefab with several images.
For some reason, the background did not scale correctly, but everything else did.
None of the advice above worked, however, this worked for me.
Save the prefab as inactive. After instantiating the game object, instantiate it.
Thus:
//Make sure the prefab is not active public GameObject currentBackground = null; public GameObject prefab; public GameObject template; // placeholder for object
currentBackground = (GameObject)Instantiate(prefab);
if (currentBackground != null)
{
currentBackground.transform.SetParent(template..transform.parent, false);
currentBackground.transform.position = template.transform.position;
currentBackground.SetActive(true);
}