Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Sebhelyes · Feb 21, 2014 at 08:28 PM · javascriptprefabdestroy

Destroy Prefab using Javascript

Hi, I'd like to ask how can I destroy a prefab using Javascript. What i am trying to achieve is that when a bullet or projectile is fired the prefab will be removed from the scene during gameplay. This is working until the second I have the prefab as a gameobject in the scene but when it is spawning from a certain coordinate they're immune to the destroy command. This is the code I am using:

function OnCollisionEnter(collision : Collision)

{

if(collision.gameObject.name == "EnemyUnit")

{

     Destroy(gameObject.Find("EnemyUnit"),0);
 }

}

I have a capsule collider attached and a rigidbody component to the prefab of "EnemyUnit". I don't know how to call a prefab in java to be targeted by the command. Could anyone help me please? YouTube and Google seem to be stuck on this one as well.

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

Answer by instruct9r · Feb 21, 2014 at 09:11 PM

Since you are using OnCollisionEnter, there is no need to use the Find, to have the object, because "Find" is taking a lot of resources from your CPU to search for that object.

 function OnCollisionEnter (collision : Collision)
 {
        if (collision.gameObject.name == "EnemyUnit")
        {
             Destroy(collision.gameObject, 0);
        }
 }

you allready have the object stored in the collision variable, so just use it :).

Anyway, if i understand correct you want to shoot at certain object and destroy it, when the bullet hits that object correct? If so, it's better to use Raycast because if the bullet is moving too fast Unity might skip the collision because of the FrameRate. If you use raycast you will simply destroy, what's infront the ray and there will be no way to miss it.

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 Sebhelyes · Feb 22, 2014 at 04:06 AM 0
Share

I applied the change as you recommended but nothing is happening. The "EnemyUnit" just bounces off the projectiles ins$$anonymous$$d of getting destroyed. I attached the script to the projectile and the EnemyUnit separetely but no change.

What I am essentially trying to do is the character of a prefab gets destroyed upon contact with a bullet. But I've been siftin through several YouTube tutorials and google researching and of course unity forum and unity wiki but their method seem to be functional and working. But when I try to do the exact same thing nothing is happening. I've spent over twenty hours to find a solution but nothing.

avatar image
0

Answer by whydoidoit · Feb 22, 2014 at 04:08 AM

If you are instantiating the EnemyUnits their name will not be EnemyUnit it will be EnemyUnit(Clone). Using tags for this would be a preferable approach.

BTW it's Javascript or Unity Script not Java - Java is a very different language that shares pretty much just the first part of its name with Javascript. I've modified your question to help out future visitors.

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

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

20 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

Related Questions

Instantiate prefabs just before it comes into view 2 Answers

Instantiate prefabs before it comes into view 0 Answers

Rigidbody2D getting destroyed in the inspector 2 Answers

Destroy a GameObject 2 Answers

Check if child exists and instantiate as child 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