- Home /
spawn an object at the location of an empty gameobject
How do I spawn an object at the location of an empty gameobject? This c# code will not be attatched to the empty gameobject location I want the object spawned.
Answer by SlowCircuit · Jun 10, 2018 at 03:30 AM
Instantiate the object at that position. https://unity3d.com/learn/tutorials/topics/scripting/instantiate
If it's a premade object, you make a public variable for it and assign it in the inspector. To get the empty gameobject's position, make a public variable for it as well and assign that in the inspector. So something like...
public Transform SpawnPosition;
public GameObject ObjectToCreate;
void SpawnObject() {
Instantiate(ObjectToCreate, SpawnPosition.position);
}
@SlowCircuit This is not working for me : ( error CS1503: Argument 2: cannot convert from 'UnityEngine.Vector3' to 'UnityEngine.Transform'
it seems that you are feeding the transform directly instead of the position, put .position after the transform variable
sorry where? using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CameraClipping : MonoBehaviour { public Transform SpawnPosition; public GameObject ObjectToCreate;
void SpawnObject()
{
Instantiate(ObjectToCreate, SpawnPosition.position);
}
}
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Spawn Script 1 Answer
c# - Rename Instatiated GameObject - Solved 2 Answers
(C#)Place GameObject from Mouse 0 Answers
Multiple Cars not working 1 Answer