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

Instantiated GameObject doesn't have animator or script enabled and has 0 health

Discussion in 'Scripting' started by ArcaneWarrior13, Feb 5, 2022.

  1. ArcaneWarrior13

    ArcaneWarrior13

    Joined:
    Apr 30, 2020
    Posts:
    8
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Target : MonoBehaviour
    5. {
    6.     public float health = 50f;
    7.     public GameObject target;
    8.     private float maxHealth;
    9.     public Animator animatorTarget;
    10.     public float timeToDespawn = 0f;
    11.     public float timeToRespawn = 3f;
    12.  
    13.     private void Start(){
    14.         maxHealth = health;
    15.     }
    16.  
    17.     public void TakeDamage(float amount){
    18.         health -= amount;
    19.         animatorTarget.SetBool("Hit", true);
    20.         StartCoroutine(Reset(amount));
    21.         if(health <= 0f){
    22.             StartCoroutine(Die());
    23.         }
    24.     }
    25.  
    26.     IEnumerator Die(){
    27.         Destroy(gameObject);
    28.         target = (GameObject)Instantiate(target, target.transform.position, target.transform.rotation);
    29.         health = maxHealth;
    30.         this.GetComponent<Target>().enabled = true;
    31.         yield return null;
    32.     }
    33.  
    34.     IEnumerator Reset(float subtract){
    35.         yield return new WaitForSeconds(1.6f);
    36.         animatorTarget.SetBool("Hit", false);
    37.         health -= subtract;
    38.         if(health <= 0f){
    39.             StartCoroutine(Die());
    40.         }
    41.     }
    42. }
    43.  
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Solving other peoples' problems in form of a lazily described puzzle with an undefined solution.
    My favorite pastime.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    Mine too... and for @AtticGame, let me be more explicit, considering that NONE of us can read your mind.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    You must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  4. ArcaneWarrior13

    ArcaneWarrior13

    Joined:
    Apr 30, 2020
    Posts:
    8
    I'm sorry, I'm kind of new to this and this is actually my first post here
     
  5. ArcaneWarrior13

    ArcaneWarrior13

    Joined:
    Apr 30, 2020
    Posts:
    8
    Thank you for being a useful member of this community and actually helping me, I got it working now!
     
    Kurt-Dekker likes this.
  6. emkay4597

    emkay4597

    Joined:
    Sep 16, 2013
    Posts:
    14
    glad you got it sorted. i share your frustration.