Beginner need help for issues

Hello !

I’m very new at Unity development (but not at C#) and i’m trying to make a simple game.
The purpose is a square who vanish another square when he touch them, but die when he get touche by them.

So i’m experimenting somes difficulties.
I based my projet on the tutoriel “Rogue like” from Unity youtube channel, I reused the code that I thought was useful so in my projet, it could have some “wtf think” for expert people like you…

The difficulties are :

  • I dontt succeed to make my enemy move for the amount of mouvement point their have.
  • I don’t succeed to destroy my enemy when it is touch by my player object.
  • I succeed to disabled it, but when my player touch it, the triggered function is also call in my enemy script, which call gameover…

That’s my principal difficilties atm, can you help me to solve it ?

PS : sorry for my english…

thank you in advance
Wazzouille

3160791–240457–The rule of squares.rar (540 KB)

void OnTriggerEnter2D(Collider2D col)
{
Destroy(col.gameObject);
}
try this to destroy the enemy when it tuches to player.

I hope it helps.

I already tried that but doesn’t succeed :confused:
When I use that, my player can make one more move, and then enemy doesn’t move anymore

You’ll have to show some actual code for us to be able to help you narrow down the issue. Be sure to use code tags, and if you’re getting any errors, paste those exactly as they appear.

I attached my projet to my thread, but here my 4 scripts :

GameManager

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour {

    public float turnDelay = .1f;
    public static GameManager instance = null;
    public BoardManager boardScript;
    [HideInInspector] public bool playersTurn = true;

    private int level = 3;
    public static List<Enemy> enemies;
    private bool enemiesMoving;

    // Use this for initialization
    void Awake () {
        if (instance == null)
            instance = this;
        else if (instance != this)
            Destroy (gameObject);
        DontDestroyOnLoad(gameObject);
        enemies = new List<Enemy> ();
        boardScript = GetComponent<BoardManager> ();
        InitGame ();
    }

    void InitGame(){
        enemies.Clear();
        boardScript.SetupScene (level);
    }

    public void GameOver(){
        enabled = false;
    }
    // Update is called once per frame
    void Update () {
        if (playersTurn || enemiesMoving) {
            return;
        }
        StartCoroutine (MoveEnemies ());
    }

    public void AddEnemyToList(Enemy script){
        enemies.Add (script);  
    }

    IEnumerator MoveEnemies(){
        enemiesMoving = true;
        yield return new WaitForSeconds (turnDelay);
        if (enemies.Count == 0) {
            yield return new WaitForSeconds (turnDelay);
        }

        for (int i = 0; i < enemies.Count; i++) {
                enemies [i].MoveEnemy ();
                yield return new WaitForSeconds (turnDelay);
        }


        playersTurn = true;
        enemiesMoving = false;
    }
}

MovingObject

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract  class MovingObject : MonoBehaviour {

    public float movetime = 0.1f;
    public LayerMask blockingLayer;

    private BoxCollider2D boxCollider;
    private Rigidbody2D rb2D;
    private float inverseMoveTime;

    // Use this for initialization
    protected virtual void Start () {

        boxCollider = GetComponent<BoxCollider2D>();
        rb2D = GetComponent<Rigidbody2D>();
        inverseMoveTime = 1f / movetime;
    }

    protected bool Move (int xDir, int yDir, out RaycastHit2D hit){
        Vector2 start = transform.position;
        Vector2 end = start + new Vector2 (xDir, yDir);

        boxCollider.enabled = false;
        hit = Physics2D.Linecast (start, end, blockingLayer);
        boxCollider.enabled = true;

        if (hit.transform == null) {
            StartCoroutine (SmoothMovement (end));
            return true;
        }
        return false;
    }

    protected virtual void AttemptMove<T>(int xDir, int yDir) where T : Component{
        RaycastHit2D hit;
        bool canMove = Move (xDir, yDir, out hit);

        if (hit.transform == null) {
            return;
        }

        T hitComponent = hit.transform.GetComponent<T> ();
        if (!canMove && hitComponent != null)
            OnCantMove (hitComponent);
    }

    protected IEnumerator SmoothMovement (Vector3 end){
        float sqrRemainingDistance = (transform.position - end).sqrMagnitude;

        while (sqrRemainingDistance > float.Epsilon) {
            Vector3 newPosition = Vector3.MoveTowards (rb2D.position, end, inverseMoveTime * Time.deltaTime);
            rb2D.MovePosition (newPosition);
            sqrRemainingDistance = (transform.position - end).sqrMagnitude;
            yield return null;
        }
    }

    protected abstract void OnCantMove <T> (T component)
            where T : Component;
  
}

Player

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MovingObject {

    public int hp = 1;
    public int movementpoint = 1;

    private Animator animator;


    // Use this for initialization
    protected override void Start () {

        base.Start ();
    }
  
    // Update is called once per frame
    void Update () {
        if (!GameManager.instance.playersTurn)
            return;

        int horizontal = 0;
        int vertical = 0;

        horizontal = (int)Input.GetAxisRaw ("Horizontal");
        vertical = (int)Input.GetAxisRaw ("Vertical");

        if (horizontal != 0)
            vertical = 0;
        if (horizontal != 0 || vertical != 0)
            AttemptMove<Player> (horizontal, vertical);
    }

    protected override void OnCantMove<T> (T component){
        Enemy hitPlayer = component as Enemy;
        Destroy (hitPlayer);
    }

    private void OnTriggerEnter2D (Collider2D other){
        if (other.tag == "Enemy") {
            other.gameObject.SetActive (false);
        };
    }
      
    protected override void AttemptMove <T> (int xDir, int yDir){
        base.AttemptMove<T>(xDir, yDir);
        RaycastHit2D  hit;
        CheckIfGameOver();
        GameManager.instance.playersTurn = false;
    }

    private void CheckIfGameOver(){
        if (hp < 1)
            GameManager.instance.GameOver();
    }

    public void LossHp(int damage){
        hp -= damage;
        CheckIfGameOver ();
    }
}

Enemy

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MovingObject {

    public int playerDamage;
    public static int movementpoint = 3;

    private Transform target;
    private bool skipMove;

    // Use this for initialization
    protected override void Start () {
        GameManager.instance.AddEnemyToList (this);
        target = GameObject.FindGameObjectWithTag ("Player").transform;
        base.Start ();
    }

    protected override void AttemptMove<T> (int xDir, int yDir){
        //if (skipMove) {
        //    skipMove = false;
        //    return;
        //}
        base.AttemptMove<T> (xDir, yDir);

        //skipMove = true;
    }

    public void MoveEnemy(){
        for (int y = 0; y < movementpoint; y++)
        {
            int xDir = 0;
            int yDir = 0;

            if (Mathf.Abs (target.position.x - transform.position.x) < float.Epsilon) {
                yDir = target.position.y > transform.position.y ? 1 : -1;
            } else {
                xDir = target.position.x > transform.position.x ? 1 : -1;
            }

            AttemptMove<Player> (xDir, yDir);
        }
    }

    protected override void OnCantMove<T>(T component){
        Player hitPlayer = component as Player;

        hitPlayer.LossHp (playerDamage);
    }
}

Sorry, I’m not in the habit of downloading and extracting RAR files from strangers on the Internet. :stuck_out_tongue:

It’s great that you shared all your code, but this may be a case where it’s too much! I can’t really siphon off that much of my company’s time to debug the entire application for you, unfortunately.

What I’d recommend you do is add Debug.Log() messages throughout your code at points where you think the problem may be. So if you’re having trouble with getting enemies to move, anywhere where you do something with enemy movement in your code, add something like Debug.Log(“Starting enemy movement”). See which ones don’t get fired off, and then dig deeper from there.

Once you’ve narrowed it down a bit more to where the problem is, I’ll be able to help you much better.

Hello !

I already did what you told me do to, and i succed to debug my bugs :smile:

But I have a new issue i don’t know how to fix…

When my Enemy destroy my Player, my Enemy take the place of my Player.
When my Player destroy my Enemy, my Player doesn’t take the place of my Enemy, and I don’t know why …

Can you help me to find where is the issue ?

Here my scripts

MovingObject

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public abstract  class MovingObject : MonoBehaviour {

    public float movetime = 0.1f;
    public LayerMask blockingLayer;
    public Text leveltest;

    private BoxCollider2D boxCollider;
    private Rigidbody2D rb2D;
    private float inverseMoveTime;

    // Use this for initialization
    protected virtual void Start () {

        boxCollider = GetComponent<BoxCollider2D>();
        rb2D = GetComponent<Rigidbody2D>();
        inverseMoveTime = 1f / movetime;
    }

    protected bool Move (int xDir, int yDir, out RaycastHit2D hit){
        Vector2 start = transform.position;
        Vector2 end = start + new Vector2 (xDir, yDir);

        boxCollider.enabled = false;
        hit = Physics2D.Linecast (start, end, blockingLayer);
        boxCollider.enabled = true;

        if (hit.transform == null) {
            StartCoroutine (SmoothMovement (end));
            return true;
        }
        return false;
    }

    protected virtual void AttemptMove<T>(int xDir, int yDir) where T : Component{
        RaycastHit2D hit;
        bool canMove = Move (xDir, yDir, out hit);
        leveltest = GameObject.Find ("Text").GetComponent<Text> ();
        if (hit.transform == null) {
            return;
        }

        T hitComponent = hit.transform.GetComponent<T> ();
        if (hitComponent == null) {
            leveltest.text = "Hit Component is null";
        }
        if (!canMove && hitComponent != null) {
           
            leveltest.text = "Hit component n'est pas null";
            OnCantMove (hitComponent);
        }
    }

    protected IEnumerator SmoothMovement (Vector3 end){
        float sqrRemainingDistance = (transform.position - end).sqrMagnitude;

        while (sqrRemainingDistance > float.Epsilon) {
            Vector3 newPosition = Vector3.MoveTowards (rb2D.position, end, inverseMoveTime * Time.deltaTime);
            rb2D.MovePosition (newPosition);
            sqrRemainingDistance = (transform.position - end).sqrMagnitude;
            yield return null;
        }
    }

    protected void OnCantMove <T> (T component) where T : Component {
        leveltest = GameObject.Find ("Text").GetComponent<Text> ();
        leveltest.text = "OUI" + component.tag;
        if (component.tag == "Enemy") {
            leveltest.text = "Je suis passé dans le OnCantMove";
            Destroy (component.gameObject);
            GameManager.enemies.Remove (component as Enemy);
        } else if (component.tag == "Player") {
            Destroy (component.gameObject);
            //GameOver;
        }
    }
   
}

Player

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Player : MovingObject {

    public int hp = 1;
    public int movementpoint = 1;
    private Text leveltest;
    private Animator animator;


    // Use this for initialization
    protected override void Start () {

        base.Start ();
    }
   
    // Update is called once per frame
    void Update () {
        if (!GameManager.instance.playersTurn)
            return;

        int horizontal = 0;
        int vertical = 0;

        horizontal = (int)Input.GetAxisRaw ("Horizontal");
        vertical = (int)Input.GetAxisRaw ("Vertical");

        if (horizontal != 0)
            vertical = 0;
        if (horizontal != 0 || vertical != 0)
            AttemptMove<Player> (horizontal, vertical);
    }

    // protected void OnCantMove<T> (T component){
        //Enemy hitPlayer = component as Enemy;
        //leveltest = GameObject.Find("Text").GetComponent<Text>();
        //leveltest.text = "Je suis passé dans le OnCantMove";
        //Destroy (hitPlayer);
//    }

    //private void OnTriggerEnter2D (Collider2D other){
        //if (other.tag == "Enemy") {
        //    other.gameObject.SetActive (false);
        //};
        //Destroy (other.gameObject);
        //leveltest = GameObject.Find("Text").GetComponent<Text>();
        //        leveltest.text = "J'ai touché un enemi";
    //    }
       
    protected override void AttemptMove <T> (int xDir, int yDir){
        leveltest = GameObject.Find("Text").GetComponent<Text>();
        //leveltest.text = "Je suis passé 1";
        base.AttemptMove<Enemy>(xDir, yDir);
        RaycastHit2D  hit;
        CheckIfGameOver();
        GameManager.instance.playersTurn = false;
    }

    private void CheckIfGameOver(){
        if (hp < 1)
            GameManager.instance.GameOver();
    }

    public void LossHp(int damage){
        hp -= damage;
        //leveltest = GameObject.Find("Text").GetComponent<Text>();
        //leveltest.text = "Je suis passé là";
        CheckIfGameOver ();
    }
}

Enemy

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MovingObject {

    public int playerDamage;
    public static int movementpoint = 3;

    private Transform target;
    private bool skipMove;

    // Use this for initialization
    protected override void Start () {
        GameManager.instance.AddEnemyToList (this);
        target = GameObject.FindGameObjectWithTag ("Player").transform;
        base.Start ();
    }

    protected override void AttemptMove<T> (int xDir, int yDir){
        //if (skipMove) {
        //    skipMove = false;
        //    return;
        //}
        base.AttemptMove<T> (xDir, yDir);

        //skipMove = true;
    }

    public void MoveEnemy(){
        for (int y = 0; y < movementpoint; y++) 
        {
            int xDir = 0;
            int yDir = 0;

            if (Mathf.Abs (target.position.x - transform.position.x) < float.Epsilon) {
                yDir = target.position.y > transform.position.y ? 1 : -1;
            } else {
                xDir = target.position.x > transform.position.x ? 1 : -1;
            }

            AttemptMove<Player> (xDir, yDir);
        }
    }

    //protected void OnCantMove<T>(T component){
//    Player hitPlayer = component as Player;

//        hitPlayer.LossHp (playerDamage);
//    }
}