Hi!!
Instantiate can do a lot of cool stuff and can be used to create unique game mechanics! It’s commonly used to spawn a copy of an object or resource inside your scene — and we’ll be doing that in this tutorial! (˶ᵔ ᵕ ᵔ˶)
I’ve prepared a video that explains how Instantiate works, along with a script to help you get started!
local InstanceObj = script.fields.InstanceObj
local position = script.fields.Position
local self = script.gameObject
local co = require("Utils.CoroutineHelper")
script.OnEnable(function()
if position == Vector3(0, 0, 0) then
position = InstanceObj.transform.position
end
local newInstance = UnityEngine.Object.Instantiate(InstanceObj, position, InstanceObj.transform.rotation)
newInstance:SetActive(true)
co.async(function()
co.wait(0.1)
self:SetActive(false)
end)
end)
local fieldDefs = {
{ name = "InstanceObj", type = "GameObject"},
{ name = "Position", type = "Vector3", default = Vector3(0, 0, 0)}
}
script.DefineFields(fieldDefs)