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

Bug error CS1061: 'HealthBar2' does not contain a definition for 'TakeDamage'

Discussion in 'Scripting' started by MertTheJojuk, Apr 6, 2024.

  1. MertTheJojuk

    MertTheJojuk

    Joined:
    Apr 15, 2023
    Posts:
    5
    I am trying to acces the take damage function from another script but it doesnt work.

    HEALTHBAR SCRIPT
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    public class HealthBar2 : MonoBehaviour
    {
    public Slider healthSlider;
    public float maxHealth = 100f;
    public float health;
    void Start()
    {
    health = maxHealth;
    }

    void Update()
    {
    if(healthSlider.value != health)
    {

    healthSlider.value = health;
    }
    }
    public void TakeDamage(float damage)
    {
    health -= damage;
    }
    }

    TAKEDAMAGE SCRIPT

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class TakeDamage : MonoBehaviour
    {
    public HealthBar2 HealthBar2;
    void Start()
    {
    HealthBar2 = GameObject.FindGameObjectWithTag("HealthBar").GetComponent<HealthBar2>();
    }
    public void OnCollisionEnter(Collision col)
    {
    if(col.gameObject.tag == "Enemy")
    {
    HealthBar2.TakeDamage(10);
    }
    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,006
    Sounds like you either have made typing mistakes or else you haven't finished the tutorial you're working from.

    You can fix your own typing mistakes. Here's how:

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly? Are you structuring the syntax correctly? Look for examples!

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.

    AND YET AGAIN, I remind you:

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    - Do not TALK about code without posting it.
    - Do NOT post unformatted code.
    - Do NOT retype code. Use copy/paste properly using code tags.
    - Do NOT post screenshots of code.
    - Do NOT post photographs of code.
    - Do NOT attach entire scripts to your post.
    - ONLY post the relevant code, and then refer to it in your discussion.