How can I create empty GameObject with name and determined RectTransform setteings within existing one?
Hello comm unity )))
How can I create empty GameObject with name and determined RectTransform setteings within existing one?
I know I can just create:
var object= new GameObject("MyObject");
But how can I place it where I want and with all that RecTransform stuff values that I need???
Answer by Losteone · May 25, 2016 at 03:19 PM
I managed this by using:
parentObj= childCount[childCount.Length - 1].transform; // searching for the last child, where I want to place my object
myObj = Instantiate(Resources.Load("Prefabs/Obj")) as GameObject;
myObj.name = "MyObj"; // setting name cuz prefab is giving (Clone) at the end
myObj.transform.parent = parentObj; // putting my object whrere I want
I notice that you constantly ask questions and take others answers and make them your own without rewarding the others that have been helping you.
I hope others read this and realize you have been using them.
Answer by Danor · Apr 27, 2017 at 03:46 PM
For anyone stumbling across this just like i did, if you just need to spawn it at the current location this is a much cleaner solution:
GameObject yourObj = Instantiate(Resources.Load("Prefabs/yourObj"), this.transform.position, this.transform.rotation) as GameObject;
yourObj.name = "yourObj";
if you need it in another object's position replace "this" with anObj
if you need specific coords
GameObject yourObj = Instantiate (yourObj, new Vector3(50, 2, 50), transform.rotation);
if you need specific rotation:
GameObject yourObj = Instantiate (object, position, Quaternion.Euler(0, 90, 0));
and if you don't need to adjust the name or anything just remove the
GameObject yourObj =
Your answer
![](https://koobas.hobune.stream/wayback/20220612081458im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Get Child GameObject's Image to set its visibility 0 Answers
All GameObjects list to a GameObject? 0 Answers
Dealing with objects and check each object name at runtime 0 Answers
How to control an object after clicking on it? 1 Answer
Need help with Descending Orbit Game Object (Plane Crashing) 0 Answers