Please help me. I’ve been trying for a few hours but I can’t get this code to work:
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.name == "Collision Test")
{
Debug.Log("I hate coding");
}
}
It keeps saying “The local function ‘OnCollisionEnter2D’ is declared but never used” and I don’t know why. I have non-kinematic rigid bodies, I’m using MonoBehavior, I just don’t know what’s wrong.
1 Like
Check this: Local functions - C# | Microsoft Learn
Since you didn’t provide the full class, it’s worse for identifying the problem.
This function is located inside redundant curly brackets: { }
You have to get it out of here.
1 Like
Here’s the full script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class slappyMovement : MonoBehaviour
{
// The stuff needed for movement :)
public Rigidbody2D rigidBody;
public float leftSpeed;
public float rightSpeed;
public float jumpHeight;
public float flapHeight;
private float flaps;
public bool facingRight;
// For handling deaths and stuff
public float deathZone;
private float lives;
private bool parachute;
// The stuff needed to check for ground, so you can jump :)
public Transform groundCheck;
public float groundCheckRadius;
public LayerMask whatIsGround;
private bool onGround;
// I like smiley faces :)
// Start is called before the first frame update :D
void Start()
{
rigidBody = GetComponent<Rigidbody2D>();
facingRight = true;
lives = 3;
parachute = false;
}
// Update is called once per frame :D
void Update()
{
// Just ground stuff
onGround = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
// For flipping the character left and right
void Flip()
{
if (transform.localEulerAngles.y != 180 && facingRight == false)
transform.Rotate(0f, 180f, 0f);
else if (transform.localEulerAngles.y != 0 && facingRight == true)
transform.Rotate(0f, -180f, 0f);
}
// Here is the problematic part
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.name == "Ground")
{
Debug.Log("I hate coding");
}
}
// For moving left
if (Input.GetKey(KeyCode.A))
{
facingRight = false;
Flip();
rigidBody.velocity = new Vector2(leftSpeed, rigidBody.velocity.y);
}
// For moving right
if (Input.GetKey(KeyCode.D))
{
facingRight = true;
Flip();
rigidBody.velocity = new Vector2(rightSpeed, rigidBody.velocity.y);
}
// For jumping
if (Input.GetKeyDown(KeyCode.W) && onGround)
{
rigidBody.velocity = new Vector2(rigidBody.velocity.x, jumpHeight);
flaps = 2;
}
// For flapping
if (Input.GetKeyDown(KeyCode.W) && onGround == false && flaps > 0 && parachute == false)
{
rigidBody.velocity = new Vector2(rigidBody.velocity.x, jumpHeight);
flaps -= 1;
}
// If the character falls off
if (transform.position.y < deathZone)
{
Debug.Log("death");
transform.position = new Vector2(-7, 5);
parachute = true;
lives -= 1;
if (lives < 0)
{
Destroy(gameObject);
}
}
// Code for the parachutes
if (parachute == true)
{
rigidBody.drag = 10;
if (onGround == true)
{
parachute = false;
}
}
else
{
rigidBody.drag = 0;
}
}
}
1 Like
In your code, OnCollisionEnter2D()
is located inside Update()
— it’s wrong — that’s why it’s compiled as a local function.
2 Likes
Thanks a lot! Whenever I look at other forums, I wasn’t able to figure that out, so I really appreciate the help
1 Like