- Home /
Question by
hamel123 · Dec 05, 2015 at 06:03 PM ·
collisioninstantiatebutton
Button onClick does not collide with object
I'm making a mobile project and there are two colliders left right and above a cube RigidBody
which control with mouse. When i press Button
cube isKinematic = false
but the problem is when it collide with collider i want to Instantiate
a new cube and then run same process. I write the code but it's not working. One cube fall and second cube is Instantiated
it doesn't perform listener. Tell me the problem that i fix this.
Cube Script:
public class CubeScript : MonoBehaviour
{
private float pos;
private bool fall;
private bool dropped;
[SerializeField]
private Rigidbody2D body;
private Button touchbutton;
void Awake ()
{
body.isKinematic = true;
fall = true;
dropped = true;
touchbutton = GameObject.FindGameObjectWithTag ("TouchButton").GetComponent<Button> ();
touchbutton.onClick.AddListener (
() => CubeShoot ()
);
}
void Update ()
{
if (fall) {
Vector3 temp = transform.position;
pos = Camera.main.ScreenToWorldPoint (Input.mousePosition).x;
temp.x = Mathf.Clamp (pos, -2.73f, 2.73f);
body.position = temp;
if (dropped) {
return;
}
}
}
public void CubeShoot ()
{
body.isKinematic = false;
}
}
Left Collider:
public class LeftCollider : MonoBehaviour
{
[SerializeField]
private BoxCollider2D collide;
[SerializeField]
private GameObject CubeClone;
void Awake ()
{
collide.isTrigger = true;
}
void OnTriggerEnter2D (Collider2D col)
{
Destroy (col.gameObject);
Instantiate (CubeClone, new Vector3 (0f, 1.934f, 0f), Quaternion.identity);
}
}
Right Collider:
public class RightCollider : MonoBehaviour
{
[SerializeField]
private BoxCollider2D collide;
[SerializeField]
private GameObject CubeClone;
void Awake ()
{
collide.isTrigger = true;
}
void OnTriggerEnter2D (Collider2D col)
{
Destroy (col.gameObject);
Instantiate (CubeClone, new Vector3 (0f, 1.934f, 0f), Quaternion.identity);
}
}
image
a4vq6.jpg
(179.5 kB)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612051437im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How To: Spawn object on raycast collision. 2 Answers
Collision with tag 5 Answers
How to instantiate on collision? 0 Answers
OnCollisionEnter Stopped Working 1 Answer