Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to create multi GameObject instances from exist GameObject

Discussion in 'Scripting' started by Tkaewkunha, Jun 29, 2016.

  1. Tkaewkunha

    Tkaewkunha

    Joined:
    Jun 28, 2016
    Posts:
    13
    Ask.PNG
    I am new with unity.

    This is my game , there are 2 game object player,and monster,

    How to create script that create multiple monsters.
    I have tried but it seem scripts didn't work.

    I have look for tutorials on youtube but there is no any tutorial show me how to do that.

    Thank you.
     
  2. Henning_Justare

    Henning_Justare

    Joined:
    Nov 10, 2014
    Posts:
    21
    Hi,

    Try searching for instantiating prefabs at runtime c#, and I'm sure you'll find something useful :)
     
  3. MineBartekSA

    MineBartekSA

    Joined:
    May 27, 2016
    Posts:
    20
    Code goes that :
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MultiMonsterScript : MonoBehaviour {
    5.  
    6.     public GameObject prefarb;
    7.     public RectTransform rectTrans;
    8.  
    9.     void Start() {
    10.         float y;
    11.         float x;
    12.         for (int i = 0; i != 10; i++) {
    13.             GameObject monster = (GameObject)Instantiate(prefarb);
    14.             monster.transform.SetParent(rectTrans, false);
    15.             x = monster.transform.localPosition.x + 10;  //Edit this what you like!
    16.             y = monster.transform.localPosition.y + 10;  //Edit this what you like!
    17.             monster.transform.localPosition = new Vector3 (x, y, 0);
    18.         }
    19.     }
    20. }
    But don't use my code like copy paste and done because you don't lern any things! This is an example now you create your code!!

    This code add to game object this game object in inspector and add to this script Prefarb and RectTransform of your map.
    To create Prefarb just grab and drag game object to a Project folder and done.

    I not tested this code i just write it for you so fix all error :D it's your homework form me :D
     
  4. Tkaewkunha

    Tkaewkunha

    Joined:
    Jun 28, 2016
    Posts:
    13
    Thank you.
     
  5. Tkaewkunha

    Tkaewkunha

    Joined:
    Jun 28, 2016
    Posts:
    13
    Thank you.
     
  6. Tkaewkunha

    Tkaewkunha

    Joined:
    Jun 28, 2016
    Posts:
    13
    how C# know What class is GameObject?
    Becuase there are 2 GameObjects in the game.
     
  7. MineBartekSA

    MineBartekSA

    Joined:
    May 27, 2016
    Posts:
    20
    This code add to game object this game object in inspector and add to this script Prefarb and RectTransform of your map.
    To create Prefarb just grab and drag game object to a Project folder and done.

    I use that allways and it's woarking. You wan't a pictures?
     
  8. Tkaewkunha

    Tkaewkunha

    Joined:
    Jun 28, 2016
    Posts:
    13
    Thank you. Now i can create multiple monster.
     
  9. MineBartekSA

    MineBartekSA

    Joined:
    May 27, 2016
    Posts:
    20
    No problem :D Just learn form my code!