Search Unity

error CS0501: `CharacterDetection.IsTouching(Collider2D)` Error (Help Needed)

Discussion in '2D' started by sethmoosepic, Mar 24, 2018.

  1. sethmoosepic

    sethmoosepic

    Joined:
    Oct 19, 2017
    Posts:
    5
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CharacterDetection : MonoBehaviour {
    6.     //change to negative for enemy
    7.     public float CharSpeed = 1f;
    8.     public Rigidbody2D rb;
    9.     public Collider2D coll;
    10.     public bool IsTouching(Collider2D collider);
    11.     void Start () {
    12.         rb = GetComponent <Rigidbody2D> ();
    13.         coll = GetComponent <Collider2D> ();
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void FixedUpdate () {
    18.         IsTouching(Collider2D);
    19.         if (coll.IsTouching) {
    20.         }
    21.         else {
    22.             rb.AddForce (Vector2.right * CharSpeed * Time.deltaTime);
    23.         }
    24.     }
    25. }
     

    Attached Files:

  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    I think you meant to use one of Unity's callback functions for collision detection:
    Code (CSharp):
    1.  
    2. //If your Collider2D component IS NOT set to "is trigger"
    3. void OnCollisionEnter2D(Collision2D coll) {
    4.  
    5. }
    6.  
    7. //If your Collider2D component IS set to "is trigger"
    8. void OnTriggerEnter2D(Collider2D coll) {
    9.  
    10. }
    11.  
    Because "public bool IsTouching(Collider2D collider);" is not a valid variable declaration, nor a valid method.