Trying to create list from Enemies colliding with Player's box collider

This is what I have so far:

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


public class Targetting : MonoBehaviour {
	public List<Transform> targets;
	public Transform selectedTarget;

	private Transform myTransform;

	// Use this for initialization
	void Start () {
		targets = new List<Transform>();
		selectedTarget = null;
		myTransform = transform;
	}
	
	private void Kosketus()
	{
		void OnCollisionEnter(Collision collision)
		{
			if (collision.gameObject.tag == "Enemy")
			{
				//stuff 
				targets.Add(collision.gameObject.transform);
			} 
		}


	}


	private void SortTargetsByDistance()
	{
		targets.Sort(delegate(Transform t1, Transform t2){
			return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
			});
		
	
	}

	private void TargetEnemy()
	{
				if (selectedTarget == null) {
						SortTargetsByDistance ();
						selectedTarget = targets [0];
				} 
				else {
					int index = targets.IndexOf(selectedTarget);	

					if(index < targets.Count - 1)
					{
						index++;
					}
					else {
						index = 0;
					}
			deselectTarget();
			selectedTarget = targets[index];
			}
			selectTarget ();
		}

	private void selectTarget()
	{
		selectedTarget.renderer.material.color = Color.red;
		PlayerAttack pa = (PlayerAttack)GetComponent("PlayerAttack");

		pa.target = selectedTarget.gameObject;
	}

	private void deselectTarget()
	{
		selectedTarget.renderer.material.color = Color.blue;
		selectedTarget = null;
	}

	// Update is called once per frame
	void Update () {
		if(Input.GetMouseButtonDown(0)) {
			Kosketus();
			TargetEnemy ();
		}
	}
}

I have attached a box collider to Player’s front and I want each “Enemy” colliding with it to be added to list when mouse1 is pressed.

After that it should sort the list by distance from target and target the first in the list.

Now I am getting error: Targetting.cs(21,37): error CS1547: Keyword ‘void’ cannot be used in this context.

So, can someone tell me how I can use the OnCollisionEnter like in the script but without getting that error and if I am even remotely close what I am trying to achieve?

I am really new to unity/c# so I am not sure what I am doing and this is a targetting script made by someone other and I am trying to edit it.

private void Kosketus()
{
  void OnCollisionEnter(Collision collision)
  {
    if (collision.gameObject.tag == "Enemy")
    {
      //stuff 
      targets.Add(collision.gameObject.transform);
     } 
  }
}

this is wrong… function in function is a nogo…

do this:

private void Kostketus()
{
  //somestuff
}

void OnCollisionEnter(Collision collision)
{
    if (collision.gameObject.tag == "Enemy")
    {
      //stuff 
      targets.Add(collision.gameObject.transform);
     } 
}

Ok, still does not work.

I have it like this atm:

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


public class Targetting : MonoBehaviour {
	public List<Transform> targets;
	public Transform selectedTarget;
	
	private Transform myTransform;
	
	// Use this for initialization
	void Start () {
		targets = new List<Transform>();
		selectedTarget = null;
		myTransform = transform;
	}
	

		void OnCollisionEnter(Collision collision)
		{
			if (collision.gameObject.tag == "Enemy")
			{
				Debug.Log ("Ebin");
				targets.Add(collision.gameObject.transform);
			}
		}
		
		

	
	
	private void SortTargetsByDistance()
	{
		targets.Sort(delegate(Transform t1, Transform t2){
			return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
		});
		
etc...
	}

But when Enemy collides with the collider it does not print anything to the console.

bumb

Seems good to me from here, there’s probably something wrong with the colliders. For a quick-test, replace if(collision.gameObject.tag == “Enemy”) with if (true), so that we can be sure that it’s something about the colliders.

Still nothing

Then there’s something wrong with the colliders, or the objects the script is attached to. Either way, without looking into your project, it’s hard to tell what’s wrong with it.