Search Unity

Question Object created from another objects

Discussion in 'Getting Started' started by qwaqwe, May 20, 2021.

  1. qwaqwe

    qwaqwe

    Joined:
    Oct 28, 2017
    Posts:
    2
    I want to create an object (for example square or any other form) consisting from number of cubes.

    Object can move, loose cubes and get cubes.

    Where to start?
     
  2. jdell64

    jdell64

    Joined:
    Mar 15, 2017
    Posts:
    13
    Hey!

    IMO, your best bet would be to have a "ManagerScript." for example, if this is for "enemies" you might make an EnemyManager that would call the "Instantiate" method on a prefab you pass into it.


    Something like


    Code (CSharp):
    1. public class Spawner : MonoBehavior {
    2.   public GameObject spawnPrefab;
    3.  
    4.   public void Spawn() {
    5.     Instantiate(spawnPrefab, transform.position, Quaternion.Identity, transform);
    6. // call spawn on your trigger or time base
    7.   }
    Make sure to set the spawnPrefab (or whatever you call it) in your Unity Editor. Instantiate is the method you likely want.
    A gotcha... if you kill the parent, the children will die as well, so make sure this is what you want. You can specify a third party parent if you want the object to live beyond the spawning parent.
     
    JoeStrout likes this.