Search Unity

it says method must have a return help

Discussion in 'Getting Started' started by jacobgeschwentner, Dec 3, 2020.

  1. jacobgeschwentner

    jacobgeschwentner

    Joined:
    Dec 3, 2020
    Posts:
    4
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Enemy : MonoBehaviour
    {
    public Transform player;
    private Rigidbody2D rb;
    public float moveSpeed = 5;
    private Vector2 movement;
    public GameObject other;


    // Start is called before the first frame update
    void Start()
    {
    rb = this.GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void Update()
    {
    Vector3 direction = player.position - transform.position;
    float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg - 90f;
    rb.rotation = angle;
    direction.Normalize();
    movement = direction;
    }
    private void FixedUpdate() {
    moveCharacter(movement);
    }
    void moveCharacter(Vector2 direction){
    rb.MovePosition((Vector2)transform.position + (direction * moveSpeed * Time.deltaTime));
    }
    OnCollisionEnter2D(Collision collision)
    {
    Destroy(GetComponent<MeshRenderer>());
    }
    }
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    It's right, you haven't declared the return type of OnCollisionEnter2D. It should be void.

    And please use the Insert Code button in the editing toolbar when you want to insert some code into a forum message. Otherwise it's almost unreadable.