How to Instantiate a Prefab Clone in Unity C# Easy Tutorial

42
14



Easy: Instantiate a Prefab in Unity when Pressing a Key and Destroy the Prefab after a Few Seconds.

Difficulty: Beginner

HOW TO INSTANTIATE and DESTROY PREFAB CLONE in C#?

What we Need to Do:

Create C# Script
Create empty game object for our script
Create public GameObject
Create GameObject Clone for our Prefab
Instantiate prefab clone
Destroy prefab clone
Test to see if the script works
Add Shoot Button/Key
Destroy our Clone, so it doesn’t sit around and waste resources
Play Test!

Have fun, keep learning and Enjoy.

Any questions feel free to ask.

Happy Coding!

Twitter:

Unity3D Indie Game Dev Podcast:

Subscribe:

Nguồn:https://wijstaanvooronzegrondrechten.org/

42 COMMENTS

  1. Thank you! Trying to figure out instantiation from a prefab was driving me nuts (Deleting the prefab, not working, not compiling, ect! This completely worked! Thank you!

  2. Dude how do you do it for UI buttons. I've been searching for weeks and weeks on end and google gives e everything but what i'm looking for. And i'm new to c# coding.

  3. what if that game object (the one you want to clone ) has some properties that you want to change before actually make it visible to the player , how do i go about it please

  4. i making a game like color switch which has many types of obstructions(circles you need to pass through) like 30 or 40 so what method should i use to generate them in the game? instanstiate destroy method or pooling method? i want the hard levels to come later in the game also how to do that?

  5. Hello and thank you so much for your cool tutorials. I need your help. I am working on 2D game and i want to destroy 2 GameObjects when they overlap and also player tap on them at that time. Kindly help me . I am searching for solution from 1 month

  6. in 2D this just doesn't work… can anybody help ? the instances appear in the hierarchy but they are nowhere to be seen in the game

  7. WHY doesn't using GetComponent work on instantiated prefab… It's like the instantiate function is return NULL. I had to cast to a Transform and use Transform newObj instead of GameObject in order to successfully GetComponent… any ideas why?

  8. Hi,mate.Great tut. Can you do similar tutorial but about UI elements?? I have been tinkering with making dynamic spawn of UIs but it happens that when i Instanciate 'em and make 'em parent to Canvas (every say I should) all starts to go crazy e.g: I have to make several changes to rectTransform instead of one to transfer it the proper size …and i cant get Event system working on those buttons I instanciate.

LEAVE A REPLY

Please enter your comment!
Please enter your name here