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. Dismiss Notice

How to duplicate a cube?

Discussion in 'Scripting' started by sam268, May 19, 2014.

  1. sam268

    sam268

    Joined:
    Apr 21, 2014
    Posts:
    149
    Hey, I am making a 3d board game. I don't want to add each tile and script it, it would be very ineffecient. So how can I make one cube, that, upon play, duplicates itself about a hundred times and spawns them all next to each other like a 10x10 board.

    please help no one responded last time.
     
  2. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    Create your tile object, with any needed scripts attached, store it as a prefab in your project.

    Create a "BoardManager.cs" script or the equivalent, which takes in a variable of type "Gameobject". Add this component to an object in your scene, drag in the prefab you created to its variable of type GameObject, then inside the Start() function of BoardManager, Create your board by instantiating the prefabs.
     
  3. sam268

    sam268

    Joined:
    Apr 21, 2014
    Posts:
    149
    Ok that helps alot thanks!
     
  4. MyIsaak

    MyIsaak

    Joined:
    May 23, 2013
    Posts:
    49
    Okay dokey. I made a script like this before, where in a for loop i would repetedly use the GameObject.CreatePrimitive to create a cube. Here my script (Dont rage at me if there is a typo or some error in the script):

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BoardManager : MonoBehaviour {
    5.  
    6.     public GameObject tilePrefab;
    7.     public int x,z;
    8.    
    9.     public bool randomColor;
    10.     public Color[] colorChoices;
    11.    
    12.     public void Start () {
    13.    
    14.         for (int a = 0; a < x; a++) {
    15.             for (int b = 0; b < z; b++) {
    16.                 GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    17.                 if (randomColor) {
    18.                     cube.renderer.material.color = colorChoices[Random.Range(0, (colorChoices.Length)) ];
    19.                 }
    20.                 cube.transform.position = new Vector3(a, 0, b);
    21.             }
    22.         }
    23.     }
    24. }
     
    Last edited: May 20, 2014
  5. MysteriX

    MysteriX

    Joined:
    Apr 8, 2014
    Posts:
    54
    Thanks for posting your code. But please use
    Code (csharp):
    1. -Tags next time. It will format the code so that everybody can read it :)
     
  6. sam268

    sam268

    Joined:
    Apr 21, 2014
    Posts:
    149
    Well I am alot better at java then at c#, but this makes some sense to me. Except, from this script, it looks like you are just creating multiple cubes, than transforming them to the same position, right? Or am I missing something?

    Forgive me, I am really bad at c#...
     
  7. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    c# and js are the same where you are talking....
    Code (csharp):
    1.  
    2. cube.transform.position = new Vector3(a, 0, b);
    3.  
    can be used in JS, and it means the same... place a cube at every point from 0 to x and 0 to y in a grid.

    the a++ and b++ mean that they add 1 each go around in the for statement. ( I am sure you know that )

    hence, they are not put in the same place.