OnCollisionEnter for script-created objects

I’m creating a simple shooting game that creates and destroys Bullets when firing on both sides (me and enemy).

public class RotateAndFire : MonoBehaviour {
	public float rotateSpeed;
	public float reloadTime;
	public float fireSpeed;
	ArrayList cubes = new ArrayList();
	float timer = 0.0f;
	void Update () {
		transform.Rotate(new Vector3(0.0f, 0.0f, rotateSpeed * Time.deltaTime));
		timer += Time.deltaTime;
		if (timer >= reloadTime) {
			timer = 0.0f;
			GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
			cube.name = "BulletEnemy";
			cube.rigidbody.constraints = RigidbodyConstraints.FreezeAll;
			cube.renderer.material.color = Color.blue;
       		cube.transform.position = transform.position;
			cube.transform.localScale = new Vector3(0.5f, 3.0f, 0.5f);
			cube.transform.eulerAngles = transform.eulerAngles;
		foreach (GameObject cube in cubes) {
			if (cube != null) {
				cube.transform.Translate(new Vector3(0.0f, fireSpeed * Time.deltaTime, 0.0f));

When I want to check if my bullets collides with my player - it’s easy - i test it by OnCollisionEnter on my player, but if my bullet (script-created) shoots their bullet (also script-created) I don’t know how to check collisions, beacause both of them are created in code and cannot check collisions. Is there any way to check collisions of 2 script-created game objects?


Just instantiate a prefab with all the needed scripts already on it (bullet with collision scripts).
Or just add the needed script via code also yourBullet.AddComponent<"CollisionScript>() (remove the ’ " ', UnityAnswers-bug).

(and just a tip, make sure that the bullets still get destroyed after a while, if they didn’t hit something)

Instantiate bullets as prefabs (they will keep scripts and functionality) but then remember to check collision by tag not name because their name will change to yourname(clone)(…)