-1
\$\begingroup\$

Here is the army code:

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

public class Army : MonoBehaviour
{
    public int ArmySize = 100;

    private Vector2 lastClickedPos;
    private bool moving = false;
    public float speed;

    public SpriteRenderer sprite;

    private GameObject Village;

    private void Start()
    {
        Village = GameObject.FindGameObjectWithTag("Village");
    }

    private void Update()
    {
        if(Input.GetMouseButton(0))
        {
            sprite.enabled = true;
            lastClickedPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            moving = true;
        }

        if(moving && (Vector2)transform.position != lastClickedPos)
        {
            float step = speed * Time.deltaTime;
            transform.position = Vector2.MoveTowards(transform.position, lastClickedPos, step);
        }
        else
        {
            moving = false;
        }

        if(ArmySize < 0)
        {
            ArmySize = 0;
        }
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if(other.gameObject.CompareTag("Village"))
        {
            Debug.Log("Attack");

            if (ArmySize <= Village.GetComponent<Village>().strength)
            {
                Debug.Log("Lost");
                ArmySize -= 30 % Village.GetComponent<Village>().strength;
            }

            if (ArmySize > Village.GetComponent<Village>().strength)
            {
                Debug.Log("Won");
                ArmySize += 15% Village.GetComponent<Village>().strength;
                Destroy(other.gameObject);
            }
        }
    }
}

and here is the village code:

using UnityEngine;

public class Village : MonoBehaviour
{
    public int strength;
    private int x;

    private void Start()
    {
        StrengthValue();
        strength = x;

        Debug.Log(gameObject.name + " ---- " + strength);

        transform.localScale = new Vector3(strength / 100, strength / 100, 1);

        if(transform.localScale.x < 1 && transform.localScale.y < 1)
        {
            Debug.Log(gameObject.name +  "Is Destroyed");
            Destroy(gameObject);
        }
    }

    private void StrengthValue()
    {
       x = Random.Range(10, 500);
    }
}

the problem is that it sometimes doesn't destroy the village even though the army size is bigger than the village's strength. I also got an error but I can't seem to re-create it.

\$\endgroup\$
1
  • \$\begingroup\$ I think I might have fixed it but I'll leave this question here if someone wants to answer it. \$\endgroup\$
    – DavidA_
    Aug 29, 2021 at 20:05

1 Answer 1

2
\$\begingroup\$

if (ArmySize <= Village.GetComponent<Village>().strength)

This code looks like it's trying to compare "this" army's size against the strength of the village it just collided with. But that's not what's happening.

Let's look at where you assign a value to the Village variable. It's nowhere inside the collision handler. It's way back in the Start method!

private void Start()
{
    Village = GameObject.FindGameObjectWithTag("Village");
}

So this army, when it comes into existence, selects the first village in the scene, and decides to do all future comparisons of army size vs village strength against that one village, no matter which village it's actually trying to attack.

This is obviously ridiculous. Compare against the village you're attacking.

private void OnTriggerEnter2D(Collider2D other)
{        
    if(!other.TryGetComponent(out Village targetVillage))
        return;
   
    Debug.Log("Attack");

    if (ArmySize <= targetVillage.strength)
    {
        Debug.Log("Lost");
        ArmySize -= 30 % targetVillage.strength;
    } else {
        Debug.Log("Won");
        ArmySize += 15 % targetVillage.strength;
        Destroy(other.gameObject);
    }
}

Notice another strength of this approach is that you don't need to compare tags anymore. You know something is a village when it has a Village component.

You probably don't want those % modulo operators in there either. They're not computing percentages, but rather remainders when 30 or 15 is divided by the village's strength. It looks more like you want something along the lines of Mathf.RoundToInt(0.30f * targetVillage.strength) to get 30% of the village's strength rounded to an integer.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .