I am making a 2d platformer where I want to be able to put out a cube with a boxcollider on on a certain keypress.
This is the current code:
using UnityEngine;
using System.Collections;
public class KillScript : MonoBehaviour {
public BoxCollider2D sc;
void Update() {
if(Input.GetKey(KeyCode.Q)) {
var pos = Input.mousePosition;
pos.z = 10;
pos = Camera.main.ScreenToWorldPoint(pos);
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.gameObject.AddComponent<BoxCollider2D>();
cube.transform.position = pos;
}
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "platform") {
Debug.Break();
}
}
}
Right now, it spawns the object and puts it in the correct area, but the boxcollider isnt added.
Why?