Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Bug Assets\Scripts\LifeSystem.cs(21,43): error CS1002: ; expected

Discussion in 'Scripting' started by RM_PT, Nov 26, 2023.

  1. RM_PT

    RM_PT

    Joined:
    Nov 26, 2023
    Posts:
    2
    I have got this error and i dont know what is wrong
    error : Assets\Scripts\LifeSystem.cs(21,43): error CS1002: ; expected

    code

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

    public class LifeSystem : MonoBehaviour
    {
    PlayerLogic player;
    public bool dead;
    public int life;
    public int maxLife;

    public Image [] heart;
    public Sprite Full;
    public Sprite Empty;


    // Start is called before the first frame update
    void Start()
    {
    player = GetComponent<PlayerLogic>
    }

    // Update is called once per frame
    void Update()
    {
    HealthLogic();
    DeadState();
    }

    void HealthLogic()
    {

    if(life > maxLife)
    {
    life = maxLife;
    }

    for (int i = 0; i < heart.Length; i++)
    {
    if(i< life)
    {
    heart.sprite = Full;
    }
    else
    {
    heart.sprite = Empty;
    }


    if(i < maxLife)
    {
    heart.enabled = true;
    }
    else
    {
    heart.enabled = false;
    }
    }
    }


    void DeadState()
    {
    if(life <= 0)
    {
    dead = true;
    player.anim.SetBool("dead", dead);
    GetComponent<PlayerLogic>().enabled = false;
    Destroy(gameObject,1.0f);
    }
    }
    }

    Can someone help me?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    8,647
    Read the error. It's telling you on line 21, character 43 that a semicolon is missing.
     
    Bunny83 likes this.
  3. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    698
    What might be helpful is an FAQ full of answers to common questions. While people may not read it at first a link to the answer could be supplied. Perhaps there is such a thing already.

    Meanwhile heart appears to be an array of images but the op isn't referencing the array element as far as I can see.
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,261
    No, that's just a result of not using proper code tags here in the forum. So a
    [i]
    would be interpreted by the markdown as an "italic tag" and if you look closely, the text following "heart" is italic text.

    So next time, please use code tags
    upload_2023-11-27_0-37-41.png
    This button will open a popup with a text field where you can paste your code in. This will properly wrap the code in code tags. Note that there's also the "inline code tag" to the right of that button. As the name suggest it allows you to insert
    inline code
    inside a line of text. Please use those only for exactly that purpose. For very small code snippets inside normal text. Whenever you want to post a block of code that is more than just a single line snippet, use the proper code tags. They actually have syntax highlighting and line numbers so it's easier to read and reference to a certain line.

    Apart from that, this is line 21 and it does not represent valid C# code:
    Code (CSharp):
    1. player = GetComponent<PlayerLogic>
    The line is not only missing the semicolon at the end, but also the actual method call brackets. At the moment you just wrote the name of a generic method and filled it's generic type argument with a type. However you don't call this method at the moment. So it should be

    Code (CSharp):
    1. player = GetComponent<PlayerLogic>();
    Without those brackets you just refer to a method without calling it. This could even be relevant in certain cases. This would be valid code, but unnecessary:
    Code (CSharp):
    1.  
    2. // create delegate foo and let it point to the bound GetComponent method. No calling brackets here
    3. System.Func<PlayerLogic> foo = GetComponent<PlayerLogic>;
    4. // here we actually call the delegate to get back the result of the method.
    5. player = foo();
    6. // Note that creating delegate allocates memory. So this is just an example and should never be used that way.
    7.  
    Haven't looked for any other errors since the code is not properly formatted and the lack of proper formatting has mangled the code already.
     
    MelvMay and Ryiah like this.