Instantiate based on distance
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);
}
}
}
Answer by Ulci · Nov 13, 2016 at 06:00 AM
Instantiate returns the newly created clone of the object. When you're checking the distance "ball" is still the original and not the instantiated clone. So instead of
Instantiate(ball, transform.position, transform.rotation);
you need to assign the new ball:
ball = Instantiate(ball, transform.position, transform.rotation);
Thanks for your help so far, Ulci. I tried what you said, but with the script as it is below, the scene starts with a flood of balls being instantiated (due to update, I'd assume). Did I put the ball assignment in the wrong spot? I tried it in different places, but those attempts resulted in either no ball or a flood of balls.
public class ReplaceBall : $$anonymous$$onoBehaviour {
public GameObject ball;
public float dangerDistance = 4f;
void Start()
{
Instantiate(ball, transform.position, transform.rotation);
}
void Update ()
{
ball = (GameObject) Instantiate(ball, transform.position, transform.rotation);
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);
}
}
}
Remove the first line in your Update method which instantiates a new ball each frame without any condition. Replace both lines
Instantiate(ball, transform.position, transform.rotation);
in Start() and inside your if block with
ball = (GameObject) Instantiate(ball, transform.position, transform.rotation);
So you have:
public GameObject ball;
public float dangerDistance = 4f;
void Start()
{
ball = (GameObject) 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)
{
ball = (GameObject) Instantiate(ball, transform.position, transform.rotation);
}
}
Your answer
Follow this Question
Related Questions
The thing you want to instantiate is null 1 Answer
Instantiate a particle system, probably a stupid question 2 Answers
Object reference not set message, directly after instantiation 1 Answer
How can I increase the speed gradually of each instantiated clone. 0 Answers
Instantiate works only in Editor 0 Answers