How to make multiple copies (clones) of a GameObject. Unity 2019 beginner tutorial
Beginner, Tutorial, Unity
0
How to clone, copy, duplicate…developers call this “to instantiate an object”. In this video we will take a look at how we can clone (instantiate) an object. It’s easy and very, very useful in almost all kinds of game development.
❤️ Subscribe to Oxmond Tutorials. Stay ahead of the game:
https://bit.ly/SubscribeOxmondTutorials
✅ Check Out the Free Asset Packages at the Unity Assets Store:
https://assetstore.unity.com/lists/top-free-packages-13201?aid=1100l4p9k
😷👕 Need a face mask / developer T-shirt? Drop by our shop and get a 20% DISCOUNT on your first purchase by using the discount code OXMONDSALE. Click here:
https://shop.oxmond.com/discount/OXMONDSALE
/* .-------. .--. .-------. .--. .--. .--.
| |--.--.--------.-----.-----.--| | |_ _|--.--| |_.-----.----|__|---.-| |-----.
| - |_ _| | _ | | _ | | | | | | _| _ | _| | _ | |__ --|
|_______|__.__|__|__|__|_____|__|__|_____| |___| |_____|____|_____|__| |__|___._|__|_____|
© OXMOND / www.oxmond.com */
using UnityEngine;
public class Coin : MonoBehaviour
{
void Update()
{
transform.Rotate(new Vector3(0f, 100f, 0f) * Time.deltaTime);
}
}
/* .-------. .--. .-------. .--. .--. .--.
| |--.--.--------.-----.-----.--| | |_ _|--.--| |_.-----.----|__|---.-| |-----.
| - |_ _| | _ | | _ | | | | | | _| _ | _| | _ | |__ --|
|_______|__.__|__|__|__|_____|__|__|_____| |___| |_____|____|_____|__| |__|___._|__|_____|
© OXMOND / www.oxmond.com */
using System.Collections.Generic;
using UnityEngine;
public class Gameplay : MonoBehaviour
{
public GameObject coinOriginal;
public GameObject coinContainer;
void Start()
{
CreateCoins(60);
}
private void CreateCoins(int coinsNum)
{
for (int i = 0; i < coinsNum; i++)
{
// GameObject CoinClone = Instantiate(coinOriginal);
GameObject CoinClone = Instantiate(coinOriginal, new Vector3(i * 0.6f, coinOriginal.transform.position.y, i * 0.75f), coinOriginal.transform.rotation);
CoinClone.name = "CoinClone-" + (i + 1);
CoinClone.transform.parent = coinContainer.transform;
}
}
public void DestroyAllCoins()
{
var coins = new List();
foreach (Transform child in coinContainer.transform) coins.Add(child.gameObject);
coins.ForEach(child => Destroy(child));
}
}