0

I create my own cube. I use below code to do it

void Start () {

    MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>();
    Mesh mesh = new Mesh ();
    meshFilter.mesh = mesh;

    mesh.vertices = new Vector3[]{
        // face 1 (xy plane, z=0)
        new Vector3(0,0,0), 
        new Vector3(1,0,0), 
        new Vector3(1,1,0), 
        new Vector3(0,1,0), 
        // face 2 (zy plane, x=1)
        new Vector3(1,0,0), 
        new Vector3(1,0,1), 
        new Vector3(1,1,1), 
        new Vector3(1,1,0), 
        // face 3 (xy plane, z=1)
        new Vector3(1,0,1), 
        new Vector3(0,0,1), 
        new Vector3(0,1,1), 
        new Vector3(1,1,1), 
        // face 4 (zy plane, x=0)
        new Vector3(0,0,1), 
        new Vector3(0,0,0), 
        new Vector3(0,1,0), 
        new Vector3(0,1,1), 
        // face 5  (zx plane, y=1)
        new Vector3(0,1,0), 
        new Vector3(1,1,0), 
        new Vector3(1,1,1), 
        new Vector3(0,1,1), 
        // face 6 (zx plane, y=0)
        new Vector3(0,0,0), 
        new Vector3(0,0,1), 
        new Vector3(1,0,1), 
        new Vector3(1,0,0), 
    };

    int faces = 6; // here a face = 2 triangles

    List<int> triangles = new List<int>();
    List<Vector2> uvs = new List<Vector2>();

    for (int i = 0; i < faces; i++) {
        int triangleOffset = i*4;
        triangles.Add(0+triangleOffset);
        triangles.Add(2+triangleOffset);
        triangles.Add(1+triangleOffset);

        triangles.Add(0+triangleOffset);
        triangles.Add(3+triangleOffset);
        triangles.Add(2+triangleOffset);

        // same uvs for all faces
        uvs.Add(new Vector2(0,0));
        uvs.Add(new Vector2(1,0));
        uvs.Add(new Vector2(1,1));
        uvs.Add(new Vector2(0,1));
    }

    mesh.triangles = triangles.ToArray();
    mesh.uv = uvs.ToArray();

    GetComponent<Renderer>().material = new Material(Shader.Find("Diffuse"));

    mesh.RecalculateNormals(); 
    mesh.RecalculateBounds (); 
    mesh.Optimize();

I want to add this object many times at runtime. I create a prefab by click right button on editor. I drag gameobject to it. I create a member variable public Gameobject prefab. I drag prefab to member variable on inspector.

I want to add 2 cube on my scene. I use below code but it doesn't work for me. It adds many objects.

enter image description here

I use below code clone and add object

prefab = gameObject;
//  for (int i = 0; i < 3; i++) {
    gO = Instantiate (prefab, new Vector3 (0 * 1.8F - 8.2f, 0, 0), Quaternion.identity) as GameObject;
//  }

How can I create a prefab from my own gameobject? How can I add it to scene at runtime properly? EDIT

If I create unity shape from editor, and I drag it to prefab, I can add many object on C# code. But I can't do same work for my own cube.

5
  • It looks like you are doing something wrong Instantiating your prefab. Do you have the script with the Instantiate Code attached to your prefab? - So the after instantiating the prefab it will recursivly instantiate the next one? Commented Jun 3, 2015 at 10:51
  • No, I have only one .cs file. And it attached to gameObject. I instantiate only one time but it recursively instantiate. Commented Jun 3, 2015 at 10:57
  • I'm not shure if i understand your setting right. - You should have one CS-File "BuildCubeMesh.cs" on your prefab. with the code in your upper example. And then you have a script "CubeBuilder.cs" With the code below in the Start-method and a reference to your prefab. Commented Jun 3, 2015 at 11:08
  • I undo my code and scene. I have a test.cs file, it draw a cube upper code. My scene has a main camera and gameObject. This cs file attached to gameObject. What should I do now? (There is not any prefab object yet) Commented Jun 3, 2015 at 11:20
  • Ok. - 1. Pull the gameObject into the "Project"-Window, it will be automatically converted into a prefab. Now delete the gameObject from your Scene. 2. Make a new empty Gameobject and add a new Script "TestFactory.cs" and add the bottom code from your post to the Start Method. 3. Add the Prefab you created earlier in the inspector => Start the Scene Commented Jun 3, 2015 at 13:45

1 Answer 1

0

So in the end you have 2 Scripts:

public class CubeFactory : MonoBehaviour
{
     public GameObject PrefabToCreate;
     // Use this for initialization
     void Start () {
     var cube = Instantiate(PrefabToCreate, new Vector3(0 * 1.8F - 8.2f, 0, 0), Quaternion.identity) as GameObject;
    }

    // Update is called once per frame
    void Update () {

    }
}

and

public class Cube : MonoBehaviour
{
    private void Start()
    {

        MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>();
        Mesh mesh = new Mesh();
        meshFilter.mesh = mesh;

        mesh.vertices = new Vector3[]
        {
            // face 1 (xy plane, z=0)
            new Vector3(0, 0, 0),
            new Vector3(1, 0, 0),
            new Vector3(1, 1, 0),
            new Vector3(0, 1, 0),
            // face 2 (zy plane, x=1)
            new Vector3(1, 0, 0),
            new Vector3(1, 0, 1),
            new Vector3(1, 1, 1),
            new Vector3(1, 1, 0),
            // face 3 (xy plane, z=1)
            new Vector3(1, 0, 1),
            new Vector3(0, 0, 1),
            new Vector3(0, 1, 1),
            new Vector3(1, 1, 1),
            // face 4 (zy plane, x=0)
            new Vector3(0, 0, 1),
            new Vector3(0, 0, 0),
            new Vector3(0, 1, 0),
            new Vector3(0, 1, 1),
            // face 5  (zx plane, y=1)
            new Vector3(0, 1, 0),
            new Vector3(1, 1, 0),
            new Vector3(1, 1, 1),
            new Vector3(0, 1, 1),
            // face 6 (zx plane, y=0)
            new Vector3(0, 0, 0),
            new Vector3(0, 0, 1),
            new Vector3(1, 0, 1),
            new Vector3(1, 0, 0),
        };

        int faces = 6; // here a face = 2 triangles

        List<int> triangles = new List<int>();
        List<Vector2> uvs = new List<Vector2>();

        for (int i = 0; i < faces; i++)
        {
            int triangleOffset = i*4;
            triangles.Add(0 + triangleOffset);
            triangles.Add(2 + triangleOffset);
            triangles.Add(1 + triangleOffset);

            triangles.Add(0 + triangleOffset);
            triangles.Add(3 + triangleOffset);
            triangles.Add(2 + triangleOffset);

            // same uvs for all faces
            uvs.Add(new Vector2(0, 0));
            uvs.Add(new Vector2(1, 0));
            uvs.Add(new Vector2(1, 1));
            uvs.Add(new Vector2(0, 1));
        }

        mesh.triangles = triangles.ToArray();
        mesh.uv = uvs.ToArray();

        GetComponent<Renderer>().material = new Material(Shader.Find("Diffuse"));

        mesh.RecalculateNormals();
        mesh.RecalculateBounds();
        mesh.Optimize();
    }
}

in your Scene you have your Camera and a GameObject with the CubeFactory on it.

Before you have to make a Prefab. Wich contains a GameObject with the "Cube.cs" Script on it.

Add the prefab as "PrefabToCreate" to your Cubefactory in the inspector.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.