Search Unity

Beginners scripting, pls Help with clones deleting

Discussion in 'Scripting' started by mansim, Apr 4, 2014.

  1. mansim

    mansim

    Joined:
    Apr 3, 2014
    Posts:
    6
    Hello. im new in Unity, only 1 week learning.. And have occured a problem which is hard to find for me in google or other sources..
    1. I want to have a Spawn (as prefab);
    2. i want, that this Spawn would create atleast 3 different Objects (prefabs) after my commands.
    And i want, that if i create a clone1, then i create clone2, the clone1 would dissapear..
    3. I want to drag on terrain several same spawns, if i press on any of them, only there created clone1 would dissapear when i create clone2 or clone3 on same spawn and so on..
    So far i have everything coded, just except the 3rd part.

    Code (csharp):
    1.  
    2. var  _mouseOver : boolean = false;
    3. var created : int = 0;
    4. var ObjectCube : GameObject;
    5. var ObjectSphere : GameObject;
    6. var PlaceTaken : int = 0;
    7. public var SpawningObject : int = 0;
    8. function Start() {
    9. gameObject.renderer.material.color = Color.blue;}
    10. function OnGUI()
    11. { if(!_mouseOver) return;}
    12. function OnMouseOver()
    13. {_mouseOver = true;}
    14. function OnMouseExit()
    15. { _mouseOver = false;}
    16. function Update() {
    17. if (_mouseOver == true  Input.GetButton("Fire1")){
    18. created = 1;
    19. gameObject.renderer.material.color = Color.red;}
    20. if (_mouseOver == false  Input.GetButton("Fire1")){
    21. created = 0;
    22. gameObject.renderer.material.color = Color.blue;}
    23. if (created == 1  PlaceTaken == 0  SpawningObject == 2){
    24. var clone = Instantiate(ObjectCube, transform.position, transform.rotation);
    25. PlaceTaken = 1;}
    26. if (created == 1  PlaceTaken == 0  SpawningObject == 3){
    27. var clone1 = Instantiate(ObjectSphere, transform.position, transform.rotation);
    28. PlaceTaken = 1;}}
    29.  
    I wish to have on the end of this script a working function like that, but i dont imagine a working example..

    Code (csharp):
    1.  
    2. if (SpawningObject == 2  Input.GetKeyDown(KeyCode.R)){  //if Cube is created, i want it to just destroy and later create a Sphere
    3. Destroy(clone.gameObject);}
    4. if (SpawningObject == 3  Input.GetKeyDown(KeyCode.R)){ // if Sphere created, i want it just destroy and later create a Cube
    5. Destroy(clone1.gameObject);}
    6.  
    But its somethink more complex i think, pls send me Link's or examples of working script, where i could delete exact there created clones... i believe its possible.. some way.. thx!
     
  2. mansim

    mansim

    Joined:
    Apr 3, 2014
    Posts:
    6
    Found out the way :)

    I created a Global, where im keeping my Enemy count information.

    And after creating every enemy, i add a number to this Global, and name my new spawned monster with that number in addition. Plus, i set my cloneA or cloneB variables to this number to remember. And when i need to destroy a clone, i set the clone number on this variable created. And it perfectly destroys :) Cant believe it worked so easy :)