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 jorchrl1 · May 08, 2012 at 10:33 AM · prefabdestroyinstancedestroy-clones

Destroy an instance of a prefab

Hello, i wrote the following script for the fireworks prefab in my game:

 var fireWorksID : int; //This Prefab unique Id to determine which fireworks effect is used!
 var speedFactor : float = 10.0;    
 var secondLimit : float = 4.0; 
 
 
 
  var target : GameObject;    
  var gameManager : GameObject;
  var gameManagerScript : GameManager;
 private var speed : float;
  var timer : float; 
 private var hit : RaycastHit;
 private var ray : Ray;
 
 
 function Start () {
     
     target = GameObject.FindWithTag("target");
     gameManager = GameObject.FindGameObjectWithTag("GameManager");
     gameManagerScript = gameManager.GetComponent("GameManager");
     transform.LookAt(target.transform.position);
     
 }
 
 
 function FixedUpdate () {
 
     if(Input.touchCount == 1) {
         ray = Camera.main.ScreenPointToRay(Input.touches[0].position);
         Debug.DrawLine(ray.origin,ray.direction * 10);
         if(Physics.Raycast(ray.origin, ray.direction * 10,hit)){
             Explode(1);
 
         }
     }
 }
 
 function Update () {
 
     timer += Time.deltaTime; //Count in seconds
 
     speed = speedFactor * Time.deltaTime;//this may go in update!
 
     
     transform.Translate(0, 0, speed);
 
     if (timer >= secondLimit) {//Time-Out
         Explode(0); //Call Explosion by time out (status = 0)
     }
 }
 
 
 function Explode (status : int){
     
     if (status == 1){//detonated
         
             //Call DetonateFirewors() function on game manager with this object's id, status and position
         //gameManagerScript.DetonateFireworks(fireWorksID,transform.position,1);
         
         }
     else if (status == 0){//timed out
 
             //Call DetonateFirewors() function on game manager with this object's id, status and position
         //gameManagerScript.DetonateFireworks(fireWorksID,transform.position,0);
         
         
         }
     else {
         
             //This one should not be called, but just in case it may be managed as a miss or maybe just "neutral"
         //gameManagerScript.DetonateFireworks(fireWorksID,transform.position,0);
                     
         Debug.Log("Somehow you managed to make a mistake!");
         }
         
         Destroy(this.gameObject);
         print("Was destroyed!?");
 
 }                        



My problem is that when in play mode i touch the prefab all the active clones are destroyed too, i want to destroy only the one i touched, however when they "timeout" they actually destroy only themselves... I hope you can help me , thanks :)

Comment
Add comment · Show 2
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 MangoDerp · May 08, 2012 at 01:38 PM 0
Share

Try ins$$anonymous$$d of setting timer variable in the editor, do it in Start(), make it a private variable and have another to set it.

avatar image hijinxbassist · May 08, 2012 at 06:45 PM 0
Share

@jorchrl1

In The Explode function, you should change the structure of your if else statements

 if(status==0)
 {
    game$$anonymous$$anagerScript.DetonateFireworks(fireWorksID,transform.position,0); 
 }
 if(status==1)
 {
     game$$anonymous$$anagerScript.DetonateFireworks(fireWorksID,transform.position,1);
 }
 else if(status!=0)  //'else' status is not 1,'if' status is not 0
 {
     Destroy(this.gameObject);
     print("Was destroyed!?");
 }

Are you destroying the object in DetonateFireworks? You should provide that code if you are destroying them there. I dont see anywhere you destroy in this code other than the "mistake" else statement.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by kolban · May 08, 2012 at 08:56 PM

In your FixedUpdate() code you are casting a ray and asking did I hit AN Object. If the answer is yes, then you explode. I think what you want to do is cast a ray and ask did I hit ME and if yes, then Explode.

It also seems to me that you are performing input processing for each instance of the GameObject ...

I can't help but feel that it would be better to have a separate (empty) Game Object to own your input processing so that the input processing is done just once. In THAT input processing, you would cast the Ray once and then if it hit an object, you would ask THAT object to explode as opposed to having all objects do input processing and ask if THEY should explode.

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 jorchrl1 · May 11, 2012 at 05:05 AM 0
Share

I think what you say about the ray casting an object is what i should do, however how do i check if the ray hits that particular instance??? Also, How can i implement that separate input processing GameObject? Sorry if sound so basic, its because im not a programmer

Thanks again for your 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

7 People are following this question.

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

Related Questions

How to destroy a game object within an instance of prefab 1 Answer

Instantiated object is destroyed in scene, but still logs as existing in console. 1 Answer

Destroy(clone) not destroying the instantiated prefab 1 Answer

How to refer to a collision between clones of the same prefab? C# 1 Answer

Problem destroying cloned 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