C# Instantiate in Unity! - Beginner Scripting Tutorial

preview_player
Показать описание
Watch this video in context on Unity Learn:

How to use Instantiate to create clones of a Prefab during runtime.
Рекомендации по теме
Комментарии
Автор

Tutorials like this are ideal for people who don't know code. I wish I had this tutorials when I started.

zentergames
Автор

Thank you, exactly what I've been looking for!

tamasorvos
Автор

Thank you, very up to the point and useful tutorial... solved my problem like water poured over fire...👍

sohrabroozbahani
Автор

The tutorial is useful for understanding how it works, but I urge you not to create and destroy bullets.
It only occupies the PC / console RAM even after destroying them.
For bullets use "Object Pooling"

giammariomarmorino
Автор

Switch to 1.5 speed and he speaks at a normal pace

litchlynn
Автор

Some of this doesn't make sense. The projectile property is declared as type Rigidbody, but aren't prefabs usually of type GameObject? At least those used by beginners? How are you passing the prefab into the projectile field?

Moreover, the line of code where you instantiate a Rigidbody to add force to will ONLY work if you have your project property typed as Rigidbody. While the instantiate method takes a Rigidbody parameter, there is apparently no implicit unboxing conversion between the two type (GameObject and Rigidbody). So this example works only for Rigidbodys passed into it?

Am I missing something?

jaimemedina
Автор

I'm a beginner and I'm confused here. Has he dragged the ball prefab (seems to be called "LemonHead" something) into the projectile rigidbody in the inspector? Is that how he creates the prefab clone by instantiating a rigidbody? Afaik rigidbody is a component of a gameobject instead of the gameobject itself.

kruth
Автор

At last 1 million downloads Congratulations

elitesenglishmayanksir
Автор

so I have 2 questions:
1) why it is declared as "Rigidbody projectile"? it's obvious that it's a GameObject.
2) why to put "as Rigidbody" when you instantiate projectile which was already declared as Rigidbody?
I'm not sure if it's Unity's mess or C#'s mess :)) there are so many options and they are not that obvious, and you have to choose all of them correctly to make it all work

kozavr
Автор

Wow! It was SO EASY - i understood nothing - really nothing! Thanx

NIKOSPARAPONIARIS
Автор

Sorry but I don't understand this code. The example code uses "As Rigidbody" to allow the Rigidbody methods to be used. But I cannot get this approach to work. I am creating the enemies in my game. if I use "enemyInstance = Instantiate(enemyPrefab, position, rotation) as Enemy; I get an error stating "cannot convert type UnityEngine.GameObject to Enemy via a reference conversion, unboxing conversion, wrapping conversion or null type conversion". If I don't have the "as Enemy" at the end then I get an error stating "cannot implicitly convert type UnityEngine.GameObject to Enemy". Obviously I cannot use the "New" syntax as you get an error stating "You are trying to create a Monobehaviour using the New keyword. This is not allowed". All I want to do is create an instance of my enemy class and be able access the enemy class methods. Am I supposed to use Instantiate and then getcomponent to do this? I have read that getcomponent is a performance killer.

cargorunnerUK
Автор

so if i wanted to make say an arrow, i could make it shoot from the bow by instantiating it and giving it motion, attaching a damage script to it and making it so when it hits a colider of another object a destroy script removes the damage so it dosent keep damaging things and resets the velocity to 0. then i could have it set itself as a child of the object it hit so it moves with it. and then have a destroy on countdown to remove itself. is that about right? or am i making it too complicated.

dante
Автор

I dont get how im suppossed to delete the objects i create. There is no way to reference them in the script because they are clones and they didnt exist until i started the game. Can anybody help me with this?

_VULTAR_
Автор

I'm confused. Why are we making another rigidbody variable when we are already importing a rigidbody?

ReptillianStrike
Автор

I would like that Unity would also offer a version of GetButtonDown that takes an enum (integer) instead of a string. It doesn't seem very efficient to me to scan a string for each inspection of a button press.

WerIstWieJesus
Автор

hi please
what is "as" and it do?
how it give position & rotation value?it attach a game object to barrelEnd.what was that

alirezaizadparast
Автор

How or where do hide the original ball?

robin
Автор

Umm I want to keep adding force to it, anyone can help?

Also I keep having the error ("MissingReferenceException: The object of type 'Rigidbody' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.")

Vetrillum
Автор

will it work if i do like this :

if(Input.GetButtonDown("FIRE 1")) {
Instantiate(projectile, barrelEnd.position, barrelEnd.rotation);
Vector2(projectileForce, 0f), ForceMode2D.Impulse);
}

LameCigar
Автор

Can anyone tell me why Unity editor go unresponsive when i use instantiate in OnMouseOver()?

commonman