0
votes

I want to make a Minecraft-like game with normal physics (ice cubes slip on the floor when you push them for example) in C# with Unity.

Right now I'm trying to destroy a glass cube when it's been hit. I already have the .fbx (animation of the cube exploding created with Blender) which works fine when I drag it on the scene and play it, but I want to play it only it at a certain time. It's not just an animation. It has objects inside the .fbx

My glass cube is a GameObject. I have a class GenMap (MonoBehaviour) and I read a config file to know the positions of the cube and the kind of cube (dirt/ice/glass/stone).

If it's a glass cube I add a component which is a breakable class in another .cs.

In this class I have an OnCollisionEnter function to destroy my cube and play the animation.

My problem is that I don't know how to get my .fbx at this moment.

I tried a public Transform object and dragged my .fbx into Unity but since I use addComponent("Breakable") my public variable is null on my object when it's been generated. I understand that when I addcomponent I attach the .cs but not the public variable I assigned manually

void AttachTextureToObject(GameObject obj, int type) //Assign a texture to an object
{
    _meshrenderer = obj.GetComponent("MeshRenderer") as MeshRenderer;
    switch (type)
    {
        case 1:
            _meshrenderer.material.mainTexture = textureG; //textureGrass i initialised in a special function
            break;
        case 2:
            _meshrenderer.material.mainTexture = textureD;
            break;
        case 3:
            _meshrenderer.material.mainTexture = textureS;
            break;
        case 4:
            _meshrenderer.material.mainTexture = textureW;
            break;
        case 5 :
            _meshrenderer.material.mainTexture = textureI;
            obj.collider.sharedMaterial = materialI;
            break;
        case 10: //glass type
            obj.AddComponent("SurfaceReflection"); //add a shader to get glass "texture"
            obj.AddComponent("Breakable"); //add my class breakable
            obj.renderer.material.shader = shaderG;
            obj.collider.sharedMaterial = materialI;
            break;
    }
}

//different .cs

public class Breakable : MonoBehaviour 
{

    public Transform breaking_cube; //when i hit play the .sc attached to my gameobject is null and if i drag my .fbx  manually and hit my cube i see the animation
    Transform current_cube;

    bool broke = false;

    void Start() //when i addComponent i initialize the current cube to itself
    {
        current_cube = this.transform;
        breaking_cube = Resources.Load("Blender/broken_cube") as Transform; //this doesn't work 
    }

    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.GetComponent<Rigidbody>())
        {
            broke = true;
            Destroy(current_cube);
            Transform broken_cube = Instantiate(breaking_cube, transform.position, Quaternion.identity) as Transform;
            current_cube = broken_cube;
            if (broke)
            {
                if (!current_cube.transform.animation.isPlaying)
                {
                    //i will destroy my gameobject when i finish
                }
            }
        }
    }
}

I tried to addcomponent an animation and add my .fbx but since it's not just an animation, it doesn't work either.

I started working with Unity recently so I probably missed or misused a function. I don't know if I'm clear enough but if you need a screenshot or something, let me know.

1

1 Answers

0
votes

Create a prefab of your Cube.

(Once in the scene, drag the gameobject back into the project hierarchy and it will become a prefab).

Now, any time you need to you can Instantiate a copy of the prefab:

public Transform CubePrefab;

void Start() 
{
  GameObject cube = (GameObject)Instantiate(CubePrefab, new Vector3(1, 1, 0), Quaternion.identity);
  AttachTextureToObject(cube, 10/*glass*/);
}

Refer to Instantiating Prefabs at runtime.