Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question If Statement activates when a function is called

Discussion in 'Scripting' started by Sodentix, Aug 3, 2021.

  1. Sodentix

    Sodentix

    Joined:
    Apr 7, 2021
    Posts:
    14
    Hello everyone. I have a problem. The problem is that I want to make an if Statement that does something when the Attack function of the Enemy is called, like "if(Attack is called) do something",but I don't know how to make an IF Statement that activates when a function from another script is called. Does anyone know to do this?
    Also sorry for the grammar, but English isn't my main language.
    Example:
    If(function is called)
    {
    TakeDamage(50);
    }
     
  2. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    I think there is a conceptual misunderstanding here:
    Why would you need something like this, and what exactly would it do as in:
    In what setting do you want to write your if-statement, is it the MonoBehaviour Update() function? Or some other function?
    Why cant the TakeDamage(50) be called by the function in question, or listen to an event invoked by the function in question?

    And when you say "function is called", what exactly do you mean?
    That the function has been called this update frame? (can be done by setting a bool)
    That the function will be called this update frame? (i dont think this can be done at all)
    That the function has been called at all? (can be done by setting a bool)
    Or That the function is currently simultaneously (parallel thread or pseudo parallel like interuptable coroutine) being called?
     
  3. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    You can't like this, it's important to understand, that unless you do something more complex, you can't have any two lines of code ran at the exact same time.
    You can do two things, either check if the function ran afterwards by setting some kind of bool flag in the function itself and checking that, or alternatively, which is the much much much better solution, if a piece of code depends on a function being called, then call that piece of code from inside the function.
    There are other ways to circumvent this issue, but it all depends on what you need this for, so elaborate.
     
  4. Sodentix

    Sodentix

    Joined:
    Apr 7, 2021
    Posts:
    14
    When the Enemy collides with the Player,the AttackFunction of the Enemy is called.Then the player should check with an if Statement if the Attack Function was called on him,when yes he should take damage
     
  5. Sodentix

    Sodentix

    Joined:
    Apr 7, 2021
    Posts:
    14
    I would make it easier that when the player collides with the enemy he gets Damage,but for some reason is collission detecting not working.It only works when the zobie collides with the Player but not when the plyer collides with the zombie
     
  6. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    there are plenty of ways to do what you want, here an easy quick and dirty example:

    expose the take-damage function of the player (as in, make it public)
    then when the enemy collides with the player, read out the player object, and in the attack function of the enemy, call the take-damage function of the player on the player object
     
  7. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Well then, time to create a health handling component. If the enemy wants to attack *something*, they get the health component of the player and call a "Damage" function or similar.
     
    Vryken likes this.
  8. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455