2

In this scene there are two boxes when user press space key box1 collide with other box then score increment. But the problem which i faces, one is when box1 collide with other box then box1 drops on the ground and it's position values doesn't stop. Second is when score increment how to respawn that box1 again.

public class box1 : MonoBehaviour {

    Rigidbody2D rigid;

    void Start () {

        rigid = GetComponent<Rigidbody2D> ();
        rigid.isKinematic = true;

    }

    void Update () {

        if (Input.GetKeyDown (KeyCode.Space)) {
            rigid.isKinematic = false;
        }
    }
}

Box 2:

public class boxer2 : MonoBehaviour {
    public int timeshit;
    public Text texter;

    // Use this for initialization
    void Start () {
        timeshit = 0;

        texter.text = "Score Fall : " + timeshit;
    }

    // Update is called once per frame
    void Update () {

    }

    void OnCollisionEnter2D(Collision2D other){
        timeshit++;
        texter.text = "Score Fall : " + timeshit;

    }

}

enter image description here

enter image description here

New Code Updated :

public class boxer2 : MonoBehaviour {

    public GameObject boxObj;
    public int timeshit;
    public Text texter;

    void Start () {
        timeshit = 0;

        texter.text = "Score Fall : " + timeshit;
    }

    void Update () {

    }

    void OnCollisionEnter2D(Collision2D col){
        timeshit++;
        texter.text = "Score Fall : " + timeshit;
        if (col.gameObject.tag == "Player") {
            Destroy(col.gameObject,0.5f);
        }
        Instantiate(boxObj, new Vector3(0.04f, 0f, 0f), Quaternion.identity);

    }

    }
2
  • Should old box1 be destroyed before another box1 spawned? Do you want to achieve stop old box1 and spawn new box1 both? or one of the choices? Nov 24, 2015 at 20:44
  • @burakKarasoy destroy old box1 and spawn new box1
    – user4935991
    Nov 24, 2015 at 20:46

1 Answer 1

0
       Declare a Public GameObject boxObj;//Don't forget to assign your box's prefab
         void OnCollisionEnter2D(Collision2D other){
            timeshit++;
            texter.text = "Score Fall : " + timeshit;
            Destroy(col.gameObject,0.1f); 
        //  if (col.gameObject.tag == "Player") {//add a tag to your box and check it before destroying a gameObject
        //  Destroy(col.gameObject,0.1f);
        //  }


         Instantiate(boxObj, new Vector3(x?F, y?, z?), Quaternion.identity);
         // x,y,z are your desired positions

        }

also check this links http://docs.unity3d.com/Manual/CreateDestroyObjects.html http://docs.unity3d.com/ScriptReference/Object.Instantiate.html

And you need to assign tour box object's reference from prefab not from hierarchy because when the object in hierarchy destroyed, it will work for first time, but second attempt the object can not be founded. You should follow thre green arrow in your situation. enter image description here

4
  • when object second time destroy then error come The object of type 'GameObject' has been destroyed but you are still trying to access it.
    – user4935991
    Nov 24, 2015 at 21:17
  • Did you add something from col in new Vector3(positions) add your last updated code. Nov 24, 2015 at 21:32
  • i updated my answer with new code and added tag Player on box1 and put box1 GameObject in box2 variable but the error still same The object of type 'GameObject' has been destroyed but you are still trying to access it
    – user4935991
    Nov 25, 2015 at 7:30
  • @tim yes i will open pc in a few hours Nov 25, 2015 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.