I'm confused

it says that Enemy.CanEnemyBeHarmed is not used.
here is the code:

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

public class Enemy : MonoBehaviour
{
    [SerializeField] float EnemyHp = 100f;

    [SerializeField] bool CanEnemyBeHarmed;

    public void Harm(float HarmHp)
    {
        if(CanEnemyBeHarmed = true);
     {
        if(EnemyHp > 0)
        {
            EnemyHp = EnemyHp - HarmHp;
        }
      
     }
    }

    void Update()
    {
      if(0 >= EnemyHp);
      {
       GetComponent(MeshRenderer).enabled = false;
      }
    }
}

Indeed, in this code, it’s declared but it’s not initialised, hence not used.

Im also betting that

        if(CanEnemyBeHarmed = true);
     {
        if(EnemyHp > 0)
        {
            EnemyHp = EnemyHp - HarmHp;
        }
    
     }

is not doing what you expect either…

if is == and the ; at the end means the below code would be run irrelevant

.

It got rid of the error, but now when I put it on the enemy the enemy disappears when the game starts.

Code here:

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

public class Enemy : MonoBehaviour
{
    [SerializeField] float EnemyHp = 100f;

    [SerializeField] bool CanEnemyBeHarmed;

    public void Harm(float HarmHp)
    {
        if(CanEnemyBeHarmed = true)
     {
        if(EnemyHp > 0)
        {
            EnemyHp = EnemyHp - HarmHp;
        }
  
     }
    }

    void Update()
    {
      if(0 >= EnemyHp);
      {
       gameObject.SetActive(false);
      }
    }
}

Always include your new code because apparently you often make mistakes and we can’t predict those. We need to see your code to tell you where you made them. Also please remove your post consists only a “.”. Try not to spam the forum with nonsense posts, please.

1 Like

You still have this:
if(CanEnemyBeHarmed = true) (line 13). In C# the = is setting a value. The == is checking equality.

1 Like

You’ve made a few errors and a couple of atypical coding style choices.

The crux of the problem is that this is not a test of the property value. You are setting it to true. Single equal symbol for assignment, double equal symbol for testing equivalency. So CanEnemyBeHarmed == true. It wouldn’t be true however and since it is a Boolean you don’t need the == true part at all.

 if(CanEnemyBeHarmed = true)

You need to recognize that a semicolon is the end of a statement and doesn’t belong in that if expression…

if(0 >= EnemyHp);

And typically you compare the value with something so EnemyHp <= 0 reads better.

so if it dies… debug it… perhaps your enemy actually doesnt start with 100hp for starters

Yet again this is another duplicate thread for the same issue here .

I can understand it can be frustrating when you start but please use a single thread and please use the Getting Started forum. I’ll move this thread and close the other.

Thanks.

1 Like