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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Where to put my Animation Bool

Discussion in 'Scripting' started by liamVAgamessss, Apr 17, 2020.

  1. liamVAgamessss

    liamVAgamessss

    Joined:
    Apr 11, 2020
    Posts:
    13
    I have this animation: anim.SetBool("isDie", true);
    that i want to put in my script but i want to have the "WaitForSeconds" action first so i have the animation before my enemy dies/ gets destroyed. I tried alot of things but i just can't figure out where to place it. I hope someone can help me. This is my script:

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

    public class Target : MonoBehaviour
    {
    public float maxHealth = 50;
    public float currentHealth;
    public float WaitForSeconds = 2.1;

    public HealthBar healthBar;

    Animator anim;

    public void TakeDamage1(float amount)
    {
    {
    currentHealth -= amount;
    if (currentHealth <= 0f)
    {
    Die();
    }
    }

    void Die()
    {
    if (WaitForSeconds <= 0)
    {
    Destroy(gameObject);
    }
    healthBar.SetHealth(currentHealth);
    }
    }

    // Start is called before the first frame update
    void Start()
    {
    currentHealth = maxHealth;
    healthBar.SetMaxHealth(maxHealth);
    }

    }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    1) Use code tags
    2) it should go inside your Die() function
     
  3. liamVAgamessss

    liamVAgamessss

    Joined:
    Apr 11, 2020
    Posts:
    13
    I did, now i got this error. Don't know what to do

    Assets\Scripts\Target.cs(9,35): error CS0664: Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Like the error says, put an "f" suffix on the literal value:
    Code (CSharp):
    1. public float WaitForSeconds = 2.1f;
     
  5. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    1) Use code tags
    2) use an 'F' suffix to create a literal of this type

    Code (CSharp):
    1.  
    2. public float WaitForSeconds = 2.1f;
    3. if (WaitForSeconds <= 0f)
    4.  
     
  6. liamVAgamessss

    liamVAgamessss

    Joined:
    Apr 11, 2020
    Posts:
    13
    Thanks!!! i was struggeling with this for over 2 weeks lmao this is kinda easy but ye with my stupid f*ck
     
  7. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    Don’t worry, everyone misses the ‘f’ now and then. If you declare a float and assign a non whole number it must have an f suffix.
     
  8. liamVAgamessss

    liamVAgamessss

    Joined:
    Apr 11, 2020
    Posts:
    13
    I'm sorry for interupting you again but when i play now i don't do damage to my enemy anymore, do you maybe know why?

    Script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Target : MonoBehaviour
    6. {
    7.     public float maxHealth = 50;
    8.     public float currentHealth;
    9.     public float WaitForSeconds = 2.1f;
    10.  
    11.     public HealthBar healthBar;
    12.  
    13.     Animator anim;
    14.  
    15.     public void TakeDamage1(float amount)
    16.     {
    17.         {
    18.             currentHealth -= amount;
    19.             if (currentHealth <= 0f)
    20.             {
    21.                 Die();
    22.             }
    23.         }
    24.  
    25.         void Die()
    26.         {
    27.             anim.SetBool("isDie", true);
    28.             if (WaitForSeconds <= 0f)
    29.             {
    30.                 Destroy(gameObject);
    31.             }
    32.             healthBar.SetHealth(currentHealth);
    33.         }
    34.     }
    35.  
    36.     // Start is called before the first frame update
    37.     void Start()
    38.     {
    39.         currentHealth = maxHealth;
    40.         healthBar.SetMaxHealth(maxHealth);
    41.     }
    42.  
    43. }
     
  9. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    Are you calling the TakeDamage1 method at all?
    You would have to call it (if you haven't) somewhere or it won't run it (ie put TakeDamage1(10) in update, set you currentHealth to something really large, and it will reduce currentHealth by 10 every frame.)
     
  10. liamVAgamessss

    liamVAgamessss

    Joined:
    Apr 11, 2020
    Posts:
    13
    No i have the TakeDamage1 in the health system
     
  11. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    Are you calling it in another script?
     
  12. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    Your curly brackets { } are a mess. Right now Die() is inside the TakeDamage1 function, it shouldn't be, plus you have extra { } earlier in TakeDamage1