I found lots of answers regarding Instantiate, but none seem to address my issue exactly.
I have a ball prefab that Instantiates at a specific point within a square room on Start (that part works fine). If the ball leaves the room, I want another prefab ball to appear where the 1st ball Instantiated (and have 1 new ball Instantiate for each ball that leaves the room, so there’s always only 1 ball in the room).
Note that I’m not destroying the balls leaving the room. Those just roll out of bounds.
I adapted some of this from a different (working) script of mine based on distance, but it’s not Instantiating when the ball leaves the “dangerDistance” distance. And I’d like this all to work without colliders, which is why I’m using distance.
What am I missing here? (Note that I have this script attached to an empty game object at the ball spawn point)
public class ReplaceBall : MonoBehaviour {
public GameObject ball;
public float dangerDistance = 4f;
void Start()
{
Instantiate(ball, transform.position, transform.rotation);
}
void Update ()
{
var distance = Vector3.Distance(ball.transform.position, transform.position);
// Activate if distance is more than dangerDistance
if (distance > dangerDistance)
{
Instantiate(ball, transform.position, transform.rotation);
}
}
}