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 kidshenlong · Feb 26, 2012 at 03:29 AM · collisionraycastprefabdestroy

Destroying a prefab with a prefab?

Hi guys,

I've been on google all night and I've just been getting more and more confused so thought I'd ask for some help.

Basically I have a game object with this script:

 public var secondsToDestroy = 5;
 private var speed = 500;
 var shotDelay = .5;
 //var instantiatedProjectile : Rigidbody;
 var projectile : Rigidbody;
 
 function Start() {
     while (true) {
         while (!Input.GetKey("space")) yield;
 
         // Fire bullet here
         var instantiatedProjectile : Rigidbody = Instantiate(projectile, transform.position, transform.rotation );
         instantiatedProjectile.velocity = transform.TransformDirection( Vector3( 0, 0, speed ) );
         yield WaitForSeconds(shotDelay);
         Destroy (instantiatedProjectile.gameObject, secondsToDestroy);
     }
 }

and this essentially launches a bullet prefab that's been attached to the script.

Now I've been trying to set up collision using raycasts which is going okay...

So I have this script attached to the bullets:

 function Update () {
     var forward = transform.TransformDirection(Vector3.forward);
     var hit : RaycastHit;    
     Debug.DrawRay(transform.position, forward * 10, Color.green);
     if(Physics.Raycast(transform.position, forward, hit, 10)){
         Debug.Log("Hit"); 
         if(hit.collider.gameObject.name == "Asteroid(Clone)"){
             Debug.Log("asteroid destroy!");
             //Code to destroy bullet 
             
             //Code to destroy asteroid
             //Destroy (this);// not working :(
         }
     }
 }

So that's all the shooting code really. I've done a lot of research but nothing too clear and a lot is really confusing as I'm new to unity so if someone could point me in the right direction I'd be really grateful.

just everything in enter to destroy the bullet or asteroid doesn't work. I'm not sure if it's because it's a prefab and I have to somehow reference the specific clone to destroy it or something else. I was thinking that maybe I should make the asteroid and bullet global values so I could access them from other script but I wasn't too sure how tbh...

Also here's the Asteroid script if it helps at all.

 var thePrefab : GameObject;
 var asteroidCount = 0;
 
 function Start () {
     for( i = 0; i < asteroidCount; ++i){
         var instance : GameObject = Instantiate(thePrefab, Vector3(Random.Range(500,1500), Random.Range(-100, 200), Random.Range(500, 3500)), Random.rotation);
         instance.transform.localScale = Vector3.one * Random.Range(8,20);
     }


Thanks again guys. Really trying to figure this out on my own but I guess every now and again you need a point in the right direction...

Comment
Add comment · Show 1
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 kidshenlong · Mar 09, 2012 at 01:18 PM 0
Share

Still can't figure this out... I can't see what I'm doing wrong :S

$$anonymous$$gestions anyone??

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Meltdown · Mar 09, 2012 at 02:41 PM

Use the instance variable from this line to destroy your instantiated object...

 var instance : GameObject = Instantiate(thePrefab, Vector3(Random.Range(500,1500), Random.Range(-100, 200), Random.Range(500, 3500)), Random.rotation);

 Destroy(instance);

So somehow you want to put your instantiated object into a GameObject variable. Then you just destroy that instance.

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 Bunny83 · Mar 09, 2012 at 03:21 PM

this referes to the script, not the the GameObject the script is attached to. To destroy the object you collided with use:

 Destroy(hit.collider.gameObject);

To destroy the bullet-gameobject use:

 Destroy(gameObject);

gameObject is a property of your script which returns the GameObject this script is attached to. Destroy(this) will only destroy your bullet script.

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

6 People are following this question.

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

Related Questions

Destroy Turret with machine Gun 0 Answers

Destroy a GameObject 2 Answers

RayCast Collision between 2 game objects (of the same prefab) 1 Answer

Collision Detection for a Prefab? 3 Answers

ray destroying prefab not clone? 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