- Home /
Instantiate creating infinite clones
I'm making a game where the player shoots a ball into baskets. I have it so that the player clicks to throw a ball. Once the ball lands in a basket or hits the floor/walls, it is destroyed. What should happen next is that another ball should appear for the player to throw. I was able to get the new ball to appear, but the script that throws the ball isn't attached to the new ball that appears. I figured out how to attach the throwing script to the new ball that appears, but each time a new ball appears it starts creating more and more clones, to the point that it starts lagging and filling up the whole screen.
When I comment out the line that adds the throwing script to the new ball, it doesn't seem to create infinite clones, but it also doesn't let me throw the new ball, so I'm not even sure if that's what's causing the infinite clones. Here's my C# code:
public class ballEnter : MonoBehaviour {
Vector3 ballLocation = new Vector3(426, 179, 80); //location where new ball should spawn
void OnTriggerEnter(Collider ball) //ball enters a bucket
{
Destroy(ball.gameObject); //ball is destroyed
GameObject newBall = (GameObject)Instantiate(ball.gameObject, ballLocation, transform.rotation); //creates a new ball
newBall.AddComponent<Throw1>(); //adds the throw script to the new ball
}
}
How can I fix the clone issue while still being able to throw the new ball? Thanks.