Search Unity

Problem with damage/gamemanager script

Discussion in '2D' started by Lamarqued, May 5, 2015.

  1. Lamarqued

    Lamarqued

    Joined:
    Jan 30, 2015
    Posts:
    9
    Hi everybody, I've got some problematics issues with some scripts.
    In fact, I'ld like to make my Player, when he collides with some Enemy, to take damage.
    I've got those scripts, one for GameManager :
    using UnityEngine;
    using System.Collections;

    public class GameManager : MonoBehaviour {

    //Vie du joueur//
    public Texture playersHealthTexture;

    //Position sur l'écran de la vie du héros//
    public float positiondecranX;
    public float positiondecranY;

    //Taille de la vie du héros//
    public int tailledeliconeX = 25;
    public int tailledeliconeY = 25;

    //Vie de départ//
    public int playersHealth = 3;

    void OnGUI(){

    //Controle la vie du joueur//
    for(int h = 0; h < playersHealth; h++){
    GUI.DrawTexture(new Rect(positiondecranX + (h * tailledeliconeX), positiondecranX,tailledeliconeX,tailledeliconeY),VieHéros,ScaleMode.ScaleToFit, true, 0);

    }
    }

    void PlayerDamaged(int damage){

    if(playersHealth > 0){
    playersHealth -= damage;
    }

    if(playersHealth <= 0){
    playersHealth = 0;
    RestartScene();
    }
    }

    void RestartScene(){
    Application.LoadLevel(Application.loadedLevel);
    }

    (Sorry for french :( )
    And in my enemy I've got :
    using UnityEngine;
    using System.Collections;

    public class MonsterDamage : MonoBehaviour {

    public GameManager gameMananger;

    int damageValue = 1;

    void OnTriggerEnter(Collider col){
    if(col.gameObject.tag == "Player"){
    gameMananger.SendMessage("PlayerDamaged", damageValue, SendMessageOptions.DontRequireReceiver);
    }
    }
    }

    Could you help me ? :'(
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,445
    Does enemy has collider set as trigger?

    Add some
    Code (CSharp):
    1. Debug.Log("ontrigger");
    inside OnTriggerEnter() to first see if any trigger collision happens?
     
  3. Witcher_CA

    Witcher_CA

    Joined:
    May 5, 2015
    Posts:
    19
    Add a trigger to the enemy
    P.S mgear alredy said .
     
  4. Lamarqued

    Lamarqued

    Joined:
    Jan 30, 2015
    Posts:
    9
    I resolved the problem, I'm in 2D, so i just add to make an OnTrigger2D §
    Thank you !