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