Search Unity

Making Objects Disappear 2D.

Discussion in 'Scripting' started by Damo.c.d, Mar 28, 2015.

  1. Damo.c.d

    Damo.c.d

    Joined:
    Mar 27, 2015
    Posts:
    4
    I'm trying to make an object move when my projectile hits it. I have it so the bullet disappears when it hits something else and I've attached the following script to the prefab which i'm having trouble with. Basically, the prefab is called EvilPlant and it has animation, and instantiates every so often (minimum 1 second, maximum 2 seconds) I tried making the prefab destroy when its hit by the bullet but then they don't spawn anymore because they delete automatically eventually. So i've made it so it just moves the prefab out of the screen but now they spawn a couple of times but eventually they stop again :/

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlantMover : MonoBehaviour {
    5.     public float minSpawnTime = 1.0f;
    6.     void OnTriggerEnter2D(Collider2D other) {
    7.         if (gameObject.name.StartsWith("EvilPlant")) {
    8.             transform.position = new Vector3(0,-10,0);
    9.          
    10.         }
    11.      
    12.     }
    13.     // Use this for initialization
    14.     void Start () {
    15.  
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update () {
    20.  
    21.     }
    22. }
    23.  
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Try separating the logic between spawning and being destroyed. Create a secondary "EvilPlantSpawner" object with a script that controls the spawning of new plants. Then let the EvilPlanet prefab worry about being killed and let the EvilPlantSpawner worry about creating new plants.

    You'll end up with something much more flexible in the end.