- Home /
How to instantiate object as child of another element
My code looks as follows:
var panel : GameObject = GameObject.Find("CalendarUI/ActivitiesUI/Drag");
var ui_renderer : SpriteRenderer;
var ui_sprite : Sprite;
var ui_texture : Texture2D;
var new_sprite : GameObject;
ui_texture = Resources.Load(image_path, Texture2D);
ui_sprite = Sprite.Create(ui_texture, Rect(0f, 0f, 48f, 48f), new Vector2(0f, 0f), 96f);
new_sprite = GameObject("My name");
new_sprite.transform.parent = panel.transform;
new_sprite.AddComponent(SpriteRenderer);
ui_renderer = new_sprite.GetComponent(SpriteRenderer);
ui_renderer.sprite = ui_sprite;
What I want to achieve is to set new sprite that is editor visible as child of panel variable. This code works as expected, but I am receiving a lot of errors like that:
NullReferenceException: Object reference not set to an instance of an object
UnityEngine.UI.LayoutGroup.CalculateLayoutInputHorizontal () (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/UI/Core/Layout/LayoutGroup.cs:44)
UnityEngine.UI.GridLayoutGroup.CalculateLayoutInputHorizontal () (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/UI/Core/Layout/GridLayoutGroup.cs:30)
UnityEngine.UI.LayoutRebuilder.<Rebuild>m__6 (UnityEngine.Component e) (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/UI/Core/Layout/LayoutRebuilder.cs:27)
UnityEngine.UI.LayoutRebuilder.PerformLayoutCalculation (UnityEngine.RectTransform rect, UnityEngine.Events.UnityAction`1 action) (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/UI/Core/Layout/LayoutRebuilder.cs:88)
UnityEngine.UI.LayoutRebuilder.UnityEngine.UI.ICanvasElement.Rebuild (CanvasUpdate executing) (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/UI/Core/Layout/LayoutRebuilder.cs:27)
UnityEngine.UI.CanvasUpdateRegistry.PerformUpdate () (at C:/BuildAgent/work/d63dfc6385190b60/Extensions/guisystem/guisystem/UI/Core/CanvasUpdateRegistry.cs:68)
UnityEngine.Canvas.SendWillRenderCanvases () (at C:/BuildAgent/work/d63dfc6385190b60/artifacts/EditorGenerated/UICanvasBindings.cs:122)
So I guess I am doing something wrong, any hint?
.transform.transform is a bit odd. While it should work its totally unneeded, use a single .transform
Which line is throwing the error?
The line causing troubles is this ->
new_sprite.transform.parent = panel.transform;
Then drop the extra .transform for starters and see if anything improves.
Well it will be fine, except that my sprite will be dangling under root. What I want is to locate it underneath of Panel.
What I want is to create sprite, exactly the same way one would do if he/she righ clicked element in editor and picked 'Create new sprite 2D'.
Answer by Drachenfels · Nov 02, 2014 at 11:10 PM
I found a source of the problem. Panel is using Grid Layout Group, so when using this:
new_sprite.transform.parent = panel.transform;
We get error as mentioned above. So now I am searching how to add sprite (or anything) to grid.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Sprite on SpriteRenderer not keyframing? 1 Answer
Translucent sprites appear solid 0 Answers
Sprites Are Blurry 0 Answers