Sunday, December 14, 2014

Unity3D Create Transform and Destroy Transform using script

Okay, let say you have a button to switch from level 1 to level 2 to level 3

each time you press the button, different enemy appear,

below is how i do it

var newenemyTransform;

var enemy_level1Transform;
var enemy_level2Transform;
var enemy_level3Transform;

//HERE YOU DRAG YOUR LEVEL 1 ENEMY PREFAB INTO enemy_level1
//DO THE SAME FOR LEVEL 2 AND LEVEL 3 ON THE OUTSIDE

IF(PRESS BUTTON)

if(mylevel==1)
{

    //IF NEW ENEMY ALREAY EXIST, YOU DESTROY IT FIRST    
if(newenemy)
    {
           Destroy(newenemy.gameObject);
    }
        

//THEN YOU CREATE ENEMY LEVEL 1 ON THE FLY
newenemy=Instantiate(enemy_level1,target.transform.position,Quaternion.identity);
}
     

else if(mylevel==2)
{
    if(newanimal)
    {
        Destroy(newenemy.gameObject);
    }
newenemy=Instantiate(enemy_level2,target.transform.position,Quaternion.identity);
     
}

No comments:

Post a Comment