0
\$\begingroup\$

I have a set of objects placed on the same layer where all the objects are invisible except one. When I drag an object into a selected area, the current dragged object gets destroyed, and the object should be loaded in the same position. I have added polygon collider and rigid body (SET FIXED ANGLE) to all the objects. The problem here is: when I drag the object to the selected area, the object gets destroyed, and the next object gets loaded, but the position varies when the object is loaded.

public Vector3 ObjectSpawnPosition1;
public GameObject obj1;
void OnCollisionEnter2D(Collision2D col) {
  if (col.gameObject.name == "SemiCircle") {

    if (Input.GetMouseButtonUp(0)) {
      scoreCalculate.currentScore += 10;
      Destroy(gameObject);
      Instantiate(obj1, ObjectSpawnPosition1, Quaternion.identity);
      obj1.renderer.enabled = true;
    }

  }
}
\$\endgroup\$
  • \$\begingroup\$ What is "ObjectSpawnPosition1" and where is it set? \$\endgroup\$ – Savlon Jul 28 '14 at 6:55
  • \$\begingroup\$ Position of the object ..iam settin this as 0.I think this s the problem of adding rigid body. I have a game object called "gameobject1" on screen . It holds an image (named "state"). when i drag a tree on a "gameobject1" collision is not detecting. so i added Rigidbody and set fixed angle. \$\endgroup\$ – Deepika C P Jul 28 '14 at 7:22
  • \$\begingroup\$ I still have no idea what you are on about sorry \$\endgroup\$ – Savlon Jul 28 '14 at 11:19
  • \$\begingroup\$ Right now you are spawning your object at ObjectSpawnPosition1. But it does not seem like you are setting the position for ObjectSpawnPosition1. Try modifying the value and see where this gets you. For example: public Vector3 ObjectSpawnPosition1 = new vector3(10,0,10); \$\endgroup\$ – Dimitri mx Aug 21 '14 at 12:19
  • 1
    \$\begingroup\$ Note that the last line there - obj1.renderer.enabled = true - is enabling the renderer of the source object or prefab, NOT the instance of it that you've just instantiated at ObjectSpawnPosition1. If you want to manipulate the new instance, you need to capture a reference to it by adding something like "GameObject myInstance = (GameObject)Instantiate(obj1..." - then you can modify myInstance. \$\endgroup\$ – DMGregory Feb 23 '15 at 20:05
1
\$\begingroup\$

Try assigning the position of the previous GameObject to the spot where you want the new one instantiated before destroying the old object.

public Vector3 objectSpawnPosition1;
public GameObject obj1;
void OnCollisionEnter2D(Collision2D col) {
if (col.gameObject.name == "SemICircle" )
{ 

        if(Input.GetMouseButtonUp(0))
        {
            scoreCalculate.currentScore+=10 ;
            objectSpawnPosition1 = gameObject.transform.position;
            Destroy(gameObject);
            Instantiate(obj1, objectSpawnPosition1, Quaternion.identity);
            obj1.renderer.enabled = true;
         }  

}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.