Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question I'm confused

Discussion in 'Getting Started' started by cookiegod19, Mar 3, 2024.

  1. cookiegod19

    cookiegod19

    Joined:
    Mar 2, 2024
    Posts:
    6
    it says that Enemy.CanEnemyBeHarmed is not used.
    here is the code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Enemy : MonoBehaviour
    6. {
    7.     [SerializeField] float EnemyHp = 100f;
    8.  
    9.     [SerializeField] bool CanEnemyBeHarmed;
    10.  
    11.     public void Harm(float HarmHp)
    12.     {
    13.         if(CanEnemyBeHarmed = true);
    14.      {
    15.         if(EnemyHp > 0)
    16.         {
    17.             EnemyHp = EnemyHp - HarmHp;
    18.         }
    19.      
    20.      }
    21.     }
    22.  
    23.     void Update()
    24.     {
    25.       if(0 >= EnemyHp);
    26.       {
    27.        GetComponent(MeshRenderer).enabled = false;
    28.       }
    29.     }
    30. }
    31.  
    32.  
     
  2. APSchmidt

    APSchmidt

    Joined:
    Oct 31, 2023
    Posts:
    262
    Indeed, in this code, it's declared but it's not initialised, hence not used.
     
  3. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,994
    Im also betting that
    Code (CSharp):
    1.         if(CanEnemyBeHarmed = true);
    2.      {
    3.         if(EnemyHp > 0)
    4.         {
    5.             EnemyHp = EnemyHp - HarmHp;
    6.         }
    7.    
    8.      }
    is not doing what you expect either..

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

    cookiegod19

    Joined:
    Mar 2, 2024
    Posts:
    6
  5. cookiegod19

    cookiegod19

    Joined:
    Mar 2, 2024
    Posts:
    6
    It got rid of the error, but now when I put it on the enemy the enemy disappears when the game starts.

    Code here:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Enemy : MonoBehaviour
    6. {
    7.     [SerializeField] float EnemyHp = 100f;
    8.  
    9.     [SerializeField] bool CanEnemyBeHarmed;
    10.  
    11.     public void Harm(float HarmHp)
    12.     {
    13.         if(CanEnemyBeHarmed = true)
    14.      {
    15.         if(EnemyHp > 0)
    16.         {
    17.             EnemyHp = EnemyHp - HarmHp;
    18.         }
    19.  
    20.      }
    21.     }
    22.  
    23.     void Update()
    24.     {
    25.       if(0 >= EnemyHp);
    26.       {
    27.        gameObject.SetActive(false);
    28.       }
    29.     }
    30. }
    31.  
    32.  
     
    Last edited: Mar 3, 2024
  6. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 5, 2024
    Posts:
    533
    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.
     
    cookiegod19 likes this.
  7. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 5, 2024
    Posts:
    533
    You still have this:
    if(CanEnemyBeHarmed = true)
    (line 13). In C# the
    =
    is setting a value. The
    ==
    is checking equality.
     
    Bunny83 likes this.
  8. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    662
    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.

    Code (CSharp):
    1.  if(CanEnemyBeHarmed = true)
    You need to recognize that a semicolon is the end of a statement and doesn't belong in that if expression..

    Code (CSharp):
    1. if(0 >= EnemyHp);
    And typically you compare the value with something so EnemyHp <= 0 reads better.
     
  9. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,994
    so if it dies.. debug it.. perhaps your enemy actually doesnt start with 100hp for starters
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,580
    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.
     
    bugfinders likes this.