How to Spawn Objects in Unity [Using Instantiate]

preview_player
Показать описание
In this video we will learn the basic way to Spawn an Object in Unity. We will make a simple C# script that will help us Instantiate an Object in Unity at runtime by pressing the Space key. If you have any questions please ask in the comments below.
Just a reminder this is not the most optimal way to do spawn objects. Object Pooling is better way to do it but it is more complicated. This is a beginner friendly tutorial. We will cover Object pooling in another video.

CODE USED IN THE VIDEO:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnerScript : MonoBehaviour
{
public GameObject cubePrefab;
void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
}
}
}

Remember that the script you want to copy this code in to has the same name "SpawnerScript" or it will not work!

#HowToSpawnObjectInUnity #SpawnInUnity #InstantiateInUnity
Рекомендации по теме
Комментарии
Автор

Thank you, sir! OUTSTANDING tutorial! You didn't try to impress with your vast knowledge of the subject, you taught a lesson and uncomplicated a complicated subject. Well done.

simonsobo
Автор

Great video! This helped me out a lot and I think this deserves more attention.

mr.cowboykat
Автор

Perfect video, exactly what i was looking for, thanks!

pianochad
Автор

Thank you so much, this helped out a ton!! Great video👍

_masterlink_
Автор

fun fact: if you want to make the spawner spawn the object where your looking at, you can just parent it with the camera (or player) and your done!

goobiewoobies
Автор

Many thanks man, this helped out a lot

EngineersToGoMT
Автор

Thank you so much, direct and forward

williamdahlstedt
Автор

thank you!!! so the solutions is GameObject+Prefab

kennethsticks
Автор

Came for a tutorial, stayed for your beautiful accent.

GlitchedBlox
Автор

my visual studio doesn't show like the instantiate options of an object in unity. helpp

samuelstefansalvatierra
Автор

Niche your video how i can so for automatic spawn and secod frome one and another game object?

micheleguardabasso
Автор

i did the exact thing but in 2d but every time i press space it spawn like 3 or 4 or spwan like 20 ones at the time which making alott how do i

yazanjordan
Автор

hey do you know how i can make the ket to spawn in the prefab e?

cerealissoup
Автор

it blew up my computer lol because i used if touch box collider instantiate and it started spawning infinitely lol but still helpfull tutorial!

RossTubes
Автор

I'm executing Instantiate in the same manner, but for some reason my instantiate is creating two at a time, instead of one. It's especially odd, considering I only have one instance of the script that spawns the game object, and it's not the same object that gets spawned that spawns the object, it's a spawner similar to this video.

richardward
Автор

I did this, but there is these things in console:
-The referenced script (Unknown) on this Beahivour is missing!
-The referenced script on this Beahivour (Game Object 'Camera) is missing
How do i fix these issues??

bossateria
Автор

How i can spawn without tapping the space bar?

chandrasekaranperformancea
Автор

Every time I press space it doubles so instead of there being 1, 2, 3, 4 there is 1, 2, 4, 8, 16. Please help

Giga_Kurtanidze
Автор

How would I make it so that if the object has been Instaniated, you are not able to spawn anymore until that object is destroyed

balaketheyoungin
Автор

when i press space, there comming thousends of cube in a second pls help me

Uwe_real