Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LadyPushkin · Nov 11, 2016 at 08:31 AM · c#instantiatecloneaddcomponent

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Attach C# Script on runtime 1 Answer

finding clones and destroying them doesn't work 0 Answers

Can I use AddComponent to add (this.script) to an object? 1 Answer

Instantiate a prefab and add a script to it 1 Answer

C# Why do I have to use .AddComponent and not "new (ClassName)"? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges