Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Harry64 · Aug 04, 2013 at 09:07 PM · prefaboncollisionenterinstantiating

OnCollisionEnter Destroy gameObject and Instantiate only 1 prefab?

I have a problem here whit this script.

I have put this script on a Cube with a Rigidbody and when the Cube falls down on the floor it destroys itselfe like I wrote in the script but it spawns 4 prefabs because the cube collides with the 4 edges on the floor in one frame.

I tryed to write a var with prefabiscreated into the OnCollisionEnter function to hopefully prevent more spawning of the prefabs but it does not work.

 #pragma strict
 private var propInfo : Prop_Info;
 
 function Awake()
 {
     propInfo = GetComponent(Prop_Info);
 }
 
 function OnCollisionEnter (collision : Collision)
 {
     var prefabiscreated : boolean = false;
     
     if(propInfo.PropBreakSettings.canbreak == false)
     return;
     
     else if (!prefabiscreated)
     {
         if (collision.relativeVelocity.magnitude > propInfo.PropBreakSettings.breakForce)
         {
             prefabiscreated = true;
             Instantiate (propInfo.PropBreakSettings.brokenObject,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 Harry64 · Aug 04, 2013 at 10:30 PM

I solved the problem very easy... stupid me. ^^;

 private var propInfo : Prop_Info;
 
 private var destroyMe : boolean = false;
 
 function Awake()
 {
     propInfo = GetComponent(Prop_Info);
 }
 
 function Update()
 {
     if (destroyMe == true)
     {
         Instantiate (propInfo.PropBreakSettings.brokenObject,transform.position, transform.rotation);
         Destroy(this.gameObject);
     }
 }
 
 function OnCollisionEnter (collision : Collision)
 {
     if(propInfo.PropBreakSettings.canBreak == false)
     return;
     
     if (collision.relativeVelocity.magnitude > propInfo.PropBreakSettings.breakForce)
     {
     destroyMe = true;
     }
 }
Comment
Add comment · 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
0

Answer by gregzo · Aug 04, 2013 at 09:25 PM

Hi,

The only way 4 prefabs would be instantiated with this code is if OnCollisionEnter is called 4 times.

Your prefabiscreated bool is useless, because it is declared in the function and will always be false, whenever OnCollisionEnter is called.

The problem is somewhere else, not in this script. By the way you should simplify your OnCollisionEnter method by removing prefabiscreated completely :

 function OnCollisionEnter (collision : Collision)
 {
     if(propInfo.PropBreakSettings.canbreak == false)
         return;
  
     if (collision.relativeVelocity.magnitude > propInfo.PropBreakSettings.breakForce)
     {
         Instantiate ( propInfo.PropBreakSettings.brokenObject, transform.position, transform.rotation );
         Destroy(this.gameObject);
     }
 }

Comment
Add comment · Show 1 · 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 Harry64 · Aug 04, 2013 at 09:30 PM 0
Share

I wrote above that the cube is hitting the floor with his flat side. so all 4 edges hit the floor at the same time so the oncollisionenter is also called 4 times in one frame (if I rotate the cube a little bit then it creates sometimes only 2 or 3 prefabs). and that is my problem. how to I tell the script that it should just instantiate this prefab 1 time.

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

14 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

Related Questions

Changing Enemy Prefab Variable (MoveSpeed) at Runtime 1 Answer

Manual: Instantiating Prefabs at Runtime 1 Answer

How to instantiate prefab when colliding with specified prefab 1 Answer

Cannot remove a prefab using OnCollisionEnter function. 1 Answer

How to Give OnCollisionEnter Priority for 2 prefabs ? 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