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\$
5
  • \$\begingroup\$ What is "ObjectSpawnPosition1" and where is it set? \$\endgroup\$
    – Savlon
    Commented Jul 28, 2014 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\$ Commented Jul 28, 2014 at 7:22
  • \$\begingroup\$ I still have no idea what you are on about sorry \$\endgroup\$
    – Savlon
    Commented Jul 28, 2014 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
    Commented Aug 21, 2014 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
    Commented Feb 23, 2015 at 20:05

1 Answer 1

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\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .