Search Unity

Problem with Spawning Script

Discussion in 'Scripting' started by fullje, Oct 21, 2014.

  1. fullje

    fullje

    Joined:
    Sep 9, 2013
    Posts:
    3
    Hi!:)

    I have 3 objects as spawn medical kits. For each object is included that script:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3.  
    4. public class firstManager : MonoBehaviour
    5. {
    6.     public PlayerHealth playerHealth;
    7.     public GameObject firstAid;
    8.     public float spawnTime = 1f;
    9.     public Transform[] spawnPoints;
    10.     public bool exist = false;
    11.  
    12.    
    13.     void Start ()
    14.     {
    15.             InvokeRepeating ("Spawn", spawnTime, spawnTime);
    16.     }
    17.  
    18.    
    19.     void Spawn ()
    20.     {
    21.                 if (playerHealth.currentHealth <= 0f)
    22.                 {
    23.                         return;
    24.                 }
    25.  
    26.  
    27.         if (exist != true)
    28.         {
    29.             int spawnPointIndex = Random.Range (0, spawnPoints.Length);
    30.            
    31.             Instantiate (firstAid, spawnPoints [spawnPointIndex].position, spawnPoints [spawnPointIndex].rotation);
    32.             exist = true;
    33.         }
    34.            
    35.  
    36.                    
    37.     }
    38. }
    39.  
    And in a 'first aid kits' is attached that script:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class FirstAidCapsuleRotator : MonoBehaviour {
    6.  
    7.     GameObject player;
    8.     PlayerHealth playerHealth;
    9.     bool playerInRange;
    10.     public int howMuchHeal=20;
    11.     GameObject firstAid;
    12.  
    13.  
    14.     void Awake ()
    15.     {
    16.         player = GameObject.FindGameObjectWithTag ("Player");
    17.         playerHealth = player.GetComponent <PlayerHealth> ();
    18.         firstAid = GameObject.FindWithTag("firstAid");
    19.     }
    20.  
    21.  
    22.  
    23.     void OnTriggerEnter (Collider other)
    24.     {
    25.         if(other.gameObject == player)
    26.         {
    27.             playerInRange = true;
    28.         }
    29.     }
    30.  
    31.     void OnTriggerExit (Collider other)
    32.     {
    33.         if(other.gameObject == player)
    34.         {
    35.             playerInRange = false;
    36.         }
    37.     }
    38.  
    39.     void Update ()
    40.     {
    41.  
    42.  
    43.         transform.Rotate (new Vector3 (20, 10, 0) * Time.deltaTime * 10);
    44.  
    45.         if(playerHealth.currentHealth < 100 && playerInRange)
    46.         {
    47.  
    48.             Heal ();
    49.  
    50.         }
    51.  
    52.     }
    53.  
    54.     void Heal ()
    55.     {
    56.         if(playerHealth.currentHealth < 100)
    57.         {
    58.             playerHealth.Healed(howMuchHeal);
    59.             Destroy (gameObject);
    60.         }
    61.         firstAid.GetComponent<firstManager> ().exist = false;
    62.     }
    63.  
    64. }
    65.  
    Now work it so that a player will take a first aid kit, she disappears and spawns but in one and the same place. I want to spawn this medical kits at all spawn points. It is from 'Nightmare project' im still learning so i have a little problems with that. Could you help me?