So on a game I am working on, I keep having the same problem where whenever I turn an object into a prefab, somehow the object always appears smaller but the width and height says otherwise. I can't seem to fix this problem and it also applies whenever I try to instantiate it, it always remains that way even when I just create a prefab.
The Prefab as an example
The image I screenshot shows what happens whenever I assign or make an object as a prefab. Am I doing something stupid? or is it just because there is a setting I am not aware of?
In unity ths scale of an object affected by it's parent scale..
so for example if I have a cube with width 10 unit and it is scale is [10,1,1]
and I convert it to prefab every time Iniciate object from it will be in the same scale..
the problem apper if my cube was a chiled for other game object with scale for example [5,5,5]...
as you see in this picture
this is the same prefab but the size is totaly diffrent because of the parent
to avoid this proble be sure always to make your prefab with scale 1 1 1
you can create an empty game object with scale 1 1 1 and position[0,0,0] then make your object child of it then make your prefab ...
:) and if you want further help pleace give more specific informations for your problem
Related
If you instantiate a prefab and parent it to an object under a UI Canvas or any RectTransform really, its scale would have "abnormal" values, in my case, they were equal to the reference Pixels Per Unit value. Why? Is this a bug or is this behavior by design?
I was having issue parenting UI items, then I started using:
rectTransform.SetParent(parentTransform, false);
and it solved it. Notice the second parameter, it indicates you do not wish the transform values to be in world space but in local space of the parent instead.
That could be it for you too.
I have a death transformation for one of my GameObjects which goes from a spherical ball to a bunch of small individual blocks. Each of these blocks I want to fade at different times but since they all use the same shader I cannot seem to figure out how to make all of them not fade out at the same time.
This first picture is the Spherical Ball in its first step for when it turns from a spherical ball to a Minecraft'ish looking block ball and to the right of it is one of the blocks that make up the Minecraft'ish looking ball shown by the red arrow.
Now this is my Inspector for one of the little blocks that make up the Minecraft'ish looking ball.
I have an arrow pointing to what makes the object fade but that is globally across all of the blocks since they use the same shader. Is it possible to have each block fade separately or am I stuck and need to find a new disappear act for the little block dudes?
You need to modify the material property by script at runtime, and you need to do it through the Renderer.material property. When you access Renderer.material, Unity will automatically create a copy of the material for you that is handled separately -- including getting its own draw call, if you care about performance. You can tell this has happened because the material name in the renderer will change to "Materialname (Instance)".
Set the material's fade property using Renderer.material.SetFloat() (or whatever the appropriate Set... function is). Unfortunately the property's name isn't "Fade Factor". You can find the property's name by looking at the shader script, or by switching the inspector to debug mode and digging through the Saved Properties array for one that looks right.
(Using Unity 4.6.0b20)
I've hit a problem where prefab button size works correctly when added in editor but seems to ignore Reference Resolution when added by script.
This uses a Canvas with Reference Resolution 1280x720 and MatchWidthOrHeight. Canvas has a Panel Vertical Layout Group for the buttons. The Button has Preferred Width/Height, and it is saved as a Prefab so new instances can be created from assets at runtime.
In the editor I can drag the prefab to scene to add instances to the panel which also has width 102 and they stack and scale nicely:
But when instead I add new instances of the prefab via script to the Panel they appear with the wrong size. Looking at the dimensions my guess is the 102 pixel size is not being scaled by the Reference Resolution:
The script code creates the instance via GameObject.Instantiate() and adds to the Panel by setting transform.parent:
GameObject uiInstance = (GameObject)GameObject.Instantiate(Resources.Load<GameObject>(assetPath));
uiInstance.transform.parent = unitButtonsPanel.transform;
I assume either there is more that needs to be done when adding the Button to the Panel than just setting Parent, or it's a bug with the beta...
Suggestions?
Use the method transform.SetParent passing false in the second parameter:
transform.SetParent(transform, false);
This way you can prevent world positioning and scaling.
You can find more information in the unity manual, searching for "UICreateFromScripting".
I found the cause -- when the prefab is added by script the Scale values are set incorrectly; in this test Scale was being set to 1.186284.
But if the script sets scale to 1.0 immediately after adding the button:
GameObject uiInstance = (GameObject)GameObject.Instantiate(Resources.Load<GameObject>(assetPath));
uiInstance.transform.parent = unitButtonsPanel.transform;
// HACK force scale to 1.0
uiInstance.transform.localScale = Vector3.one;
the new button instances are sized correctly.
imho this is a Unity bug because adding the prefab instance in the editor sets scale 1.0, the prefab itself has scale 1.0, so I can't see a reason why adding it from script should act differently. But I'm kind of guessing as there's not much documentation for the new UI yet :)
This is not a bug, its an order of execution issue. When using Unity's new layout components the values are driven by the layout components, which get calculated once at the end of the frame that a layout rebuild call is made. If you're basing anything in a script off of the new rectTransform.rect calculations which are driven by the layout elements, you'll need to wait until this calculation has been performed or call it yourself by using
LayoutRebuilder.MarkLayoutForRebuild (transform as RectTransform);
You'll still have to wait until the end of the frame for those calculations to take place before using anything from the rectTransform like scale, pos, etc. This had me stumped for a while when I first start dynamically creating UI elements and the scale was zero depending upon when I instantiated them.
You can read about that rebuild call and the order of execution of the layout calc stuff here:
http://docs.unity3d.com/Manual/UIAutoLayout.html
I am trying to grab a Gameobject that's placed in the correct spot and re-parent it to different Gameobject transforms using code. I manage to do this by using ..transform.parent = parent.transform but the rotation and position get messed up. How can I keep it's rotation and position when re-parenting?
Thanks!
Always use gameObject.transform.SetParent(anotherGameObject), or you risk exactly the sort of phenomena you've described. See SetParent docs; there is a second parameter to consider.
The form mentioned in the question still works in many cases, but has been deprecated for a while.
Place all game object that you want to parent, to empty game object(example:"EMPTY\PARENT\EMPTY\CHILD") with scale 1:1:1(in editor)or re-scale your parent game object to 1:1:1
For an assignment I was told to experiment with the Unity hierarchy by making an amusement ride. I created a surface that moves up and down that works fine. When I add new objects to the surface that moves up and down and attempt to rotate them they also scale at the same time. I'm not sure why this happens. I have tried making them outside of the root object then adding them in but it does the same thing. If someone could give me a hint as to what I'm doing wrong that would be awesome.
If they scale right in the moment when you add them to the surface, then you probably scaled the surface before. In an object hierarchy all objects inherit the transformations of their parents.
You can avoid that by creating your tree with empty objects and only attach the visible objects as leafs.
Instead of doing it this way (which will result in the sub-objects beeing scaled according to the scale of the surface):
Surface
+SubObject 1
+SubObject 2
You can do it that way:
EmptyObject (scale 1)
+Surface
+SubObject 1
+SubObject 2
That should solve your problem