Destroying the object that I am selecting

Hi everyone,

I have to objects to represent the same thing. Which one should appear depends on how close to the character it is. I currently have two scripts the first one is on the objects themselves and is doing something of the sorts:

   if (iAmNearObject && distanceToCamera>10)
    {
      this.GetComponent<MeshRenderer>().enabled=false;
      Instantiate(farObjectTransform,this.position,this.rotation);
      Destroy(this.gameObject);
      Destroy(this); //Destroy the current script
    }
    if (iAmFarObject && distanceToCamera<=10)
    {
      this.GetComponent<MeshRenderer>().enabled=false;
      Instantiate(nearObject,this.position,this.rotation);
      Destroy(this.gameObject);
      Destroy(this); //Destroy the current script
    }

This works fine and when moving back and forth the objects are hidden/deleted (I tested it without hiding the object and works fine).

I have another script attached to the camera that says:

private FocusedObject;
OnUpdate ()
{
  if(Physics.Raycast(Camera.main.transform.position,Camera...forward, out hitInfo))
  {
    FocusedObject=hitInfo.collider.gameObject;
  }
  else
  {
     FocusedObject=null;
  }
}
OnUserSelects() //User presses A for example
{
  if(FocusedObject!=null)
  {
    Destroy(FocusedObject.GetComponent<FirstScript>());
    Destroy(FocusedObject);
  }
}

This is correctly removing the object.

The problem is that after selecting the object for deletion, and moving more than 10m away, the farAway object is showing up. Which makes me believe that somehow the object instance is not being destroyed.

Any help would be appreciated

I think you need to tell your far object to become the near object, and vice versa when the distance crosses the range. For example, if you walk up to a far object, you will then be standing close to a near object, however if you walk away, that object will technically be a far object due to range, but will be hard coded as a close object. You may have to change your design. These are call LOD models… Maybe just say if, distance is less than x, use model a, if its greater, use model b,attach it to a parent empty.