Search Unity

Resolved error CS1061: 'Collider2D' does not contain a definition for 'GameObject'

Discussion in 'Scripting' started by danmedado, Jul 31, 2020.

Thread Status:
Not open for further replies.
  1. danmedado

    danmedado

    Joined:
    Jul 26, 2020
    Posts:
    6
    Getting The Error
    error: cs(30,32)CS1061: 'Collider2D' does not contain a definition for 'GameObject' and no accessible extension method 'GameObject' accepting a first argument of type 'Collider2D' could be found (are you missing a using directive or an assembly reference?)
    I need help, I just don't get why I'm getting this error. Here is the code, it is a script for my sword hitbox to damage the enemy.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class HurtEnemy : MonoBehaviour
    6. {
    7.  
    8.     public int damageToGive = 1;
    9.  
    10.  
    11.     // Start is called before the first frame update
    12.     void Start()
    13.     {
    14.  
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.  
    21.     }
    22.  
    23.  
    24.     private void OnTriggerEnter2D(Collider2D other)
    25.     {
    26.  
    27.         if (other.tag == "Enemy")
    28.         {
    29.             HealthManager eHealthMan;
    30.             eHealthMan = other.GameObject.GetComponent<HealthManager>();
    31.             eHealthMan.HurtEnemy(damageToGive);
    32.  
    33.  
    34.         }
    35.  
    36.     }
    37. }
    38.  
     
  2. danmedado

    danmedado

    Joined:
    Jul 26, 2020
    Posts:
    6
    lol "other.GameObject.GetComponent" should've been "other.gameObject.GetComponent" with a lowercase g
     
  3. Manuel5ive

    Manuel5ive

    Joined:
    Dec 27, 2020
    Posts:
    2
    ist das richtig

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

    public class NextLevel : MonoBehaviour {

    void OnTriggerEnter2D(Collider2D other)
    {
    if (other.Tag == "Spieler") {
    Debug.Log ("Nächstes Level");
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex +1);
    }
    }
    }
     
  4. Manuel5ive

    Manuel5ive

    Joined:
    Dec 27, 2020
    Posts:
    2
    weil bei mir ist immer ein fehler
     
  5. kishor1509

    kishor1509

    Joined:
    Apr 15, 2023
    Posts:
    1
    So much thanks bro
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,459
    Thanks is always appreciated but please use the like button rather than necroing a thread.

    Thanks.
     
Thread Status:
Not open for further replies.