- Home /
How can i spawn a prefab at an empty game object? C#
I tried scripting it casually but gets an error Object reference not set to an instance of an Object C#.
Please rewrite your question, it is not clear what you want. If you want to instantiate an empty GameObject, you actually don't use Instantaite, but rather just delcare a new GameObject variable and an empty GO will appear at position 0,0,0.
Do you mean something like gameObject.Position = emptyObj.Position; ? Rephrase your question please.
Answer by Hraesvelgr-tc · Sep 11, 2016 at 06:05 PM
im not sure if this helps but i think your trying to make an empty GameObject become say a map
void mapGen()
{
mapGenerator mapBuild = GetComponent<mapGenerator> () as mapGenerator;
mapBuild.generateMap();
}
so for the longest time i was getting the same null exception reference. the object in the scene the script is attached to i detached it and than in the void start i just loaded it like so.
void Start ()
{
gameObject.AddComponent<meshbuilder> ();
}
im by no means an expert but im pretty sure the null exception reference has to do with the fact your not pointing or making any reference to the actual gameobject in scene nor are you making a new one. so there is an error, not with the code itself but that to unity theres no object to change.
i meant spawning a gameobject at a specific location(empty object) like spawning point
oh ok if theres no gameobj than like cherno said just saying GameObject spawns a empty gameobj. if you want that gameobj thats empty at a specificied location than change its transform. but to make an empty obj into a cube capsule or w/e you need to change or add a mesh at least
Answer by rajan4uto · Sep 12, 2016 at 10:05 AM
GameObject obj=(GameObject)Instantiate(prefab, "empty game object position", Quaternion.identity);
if you want to spawn object as a child of empty gameobject then add this code : obj.transform.parent=emptygameobject.transform;
Answer by Bunnytoes · May 15, 2021 at 04:12 PM
make a transform for where you want them to spawn and drag the empty object into it
Your answer
Follow this Question
Related Questions
Best way to determine if there is a gameobject in multiple locations? 1 Answer
node null reference 1 Answer
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Create GameObject On Click Not Working 0 Answers
Please help. How do I add a 3D Object at a specific location in the GO Maps package? 0 Answers