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 Jdogmaster · Apr 09, 2018 at 10:34 PM · instantiatedestroycloneduplicateclones

restricting clone number

im having a problem with this script sometimes making my gameobject clone to many times, how can i make it restrict to only cloning 1 time? using System.Collections; using System.Collections.Generic; using UnityEngine;

public class destructible : MonoBehaviour {

 public GameObject debrisPrefab;
 public float strength;

 void OnMouseDown() {
     DestroyMe ();
 }

 void OnCollisionEnter(Collision collision ) {

     if (collision.impactForceSum.magnitude > strength) {
         DestroyMe ();
     }

 }
     void DestroyMe () {
         
         if (debrisPrefab) {
             
     Instantiate (debrisPrefab, transform.position, transform.rotation);
         }

     Destroy (gameObject);

 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by davidcox70 · Apr 09, 2018 at 11:11 PM

Perhaps what is happening is that the OnCollisionEnter event is firing many times as it collides. This would trigger your DestroyMe script many times, which in turn might be making multiple instantiates.

Try adding a private bool isCloned at the beginning, and then;

 void DestroyMe () {
          
          if (debrisPrefab && !isCloned) {
              
      Instantiate (debrisPrefab, transform.position, transform.rotation);
          }
     isCloned=true;
      Destroy (gameObject);
  }


This should ensure the Instantiate line can only run once.

Comment
Add comment · Show 4 · Share
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
avatar image Jdogmaster · Apr 09, 2018 at 11:25 PM 0
Share

ya thanks man ill try it out, i have a tornado game and the tornado picks up tones of objects and when it hits my house it duplicates a shattered version of the house, becasue the house has at least 50 mesh colliders if many of the colliders are hit by an object at the same time it dipplicates it that many times

avatar image Jdogmaster · Apr 09, 2018 at 11:28 PM 0
Share

do i need to make isCloned an int or public float or something?

avatar image Jdogmaster · Apr 09, 2018 at 11:34 PM 0
Share

never$$anonymous$$d didnt fully read u comment :D

avatar image Jdogmaster · Apr 09, 2018 at 11:36 PM 0
Share

Yaaaaa!!!!! it worked thanks so much

avatar image
0

Answer by Jdogmaster · Apr 09, 2018 at 11:32 PM

im getting an error with the "isCloned" public GameObject debrisPrefab; public float strength;

 void OnMouseDown() {
     DestroyMe ();
 }

 void OnCollisionEnter(Collision collision ) {

     if (collision.relativeVelocity.magnitude > strength) {
         DestroyMe ();
     }

 }
     void DestroyMe () {
         
         if (debrisPrefab && !isCloned) {
             
     Instantiate (debrisPrefab, transform.position, transform.rotation);
         }
     isCloned = true;
     Destroy (gameObject);


     
 }
Comment
Add comment · Show 4 · Share
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
avatar image Jdogmaster · Apr 09, 2018 at 11:34 PM 0
Share

never $$anonymous$$d didnt fully read u comment :D

avatar image davidcox70 · Apr 09, 2018 at 11:37 PM 0
Share

You'll need to define your isCloned at the beginning;

 private bool isCloned;
avatar image Jdogmaster · Apr 09, 2018 at 11:40 PM 0
Share

it works perfect now man thx again been trying to fix this problem like all day and turns out its just that simple :D

avatar image davidcox70 Jdogmaster · Apr 09, 2018 at 11:41 PM 0
Share

Lol. We all have days like that!

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

81 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroy a specific instantiated clone? 2 Answers

How can I destroy my Instance without renaming it? 2 Answers

Instantiate and Destroy an object while crossing a line with its clone also 1 Answer

Destroy multiple instances of an object 1 Answer

Trying to Instantiate() an object without cloning the code 2 Answers


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