• 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 EpicPandaGamer25 · Mar 18, 2015 at 04:49 PM · javascriptinstantiateprefabdeletetimed

I need one of my prefabs to be deleted after a few seconds

Code 1: (Bullet Destroy)

 #pragma strict
 
 var DestroyTime : float;
 
 function Awake () {
     Destroy(GameObject.Find("Projectile(Clone)"), DestroyTime);
 }
 

Code 2: (Bullet Spawn) #pragma strict

 var bulletspawn : Transform;
 var bulletprefab : GameObject;
 var bulletSpeed : float;
 
 
 function Start () {
 
 }
 
 function Update () {
     if (Input.GetMouseButtonUp(0)) {
         var bulletinstance = Instantiate(GameObject.Find("Projectile"),GameObject.Find("BulletSpawn").transform.position,Quaternion.identity);
         bulletinstance.rigidbody.AddForce(transform.forward * 2000);
 
     }
 }


Whenever I click left mouse button, my bullets spawn like normal, with a force of 2000 on the rigidbody. The original prefab is not deleted, but the cclones are, which is good.Whenever i spawn 2 or more bullets at the same time, none of them are deleted. How can I fix this?

Comment
Add comment · Show 9
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 AlwaysSunny · Mar 18, 2015 at 03:16 PM 2
Share

This is not a wise approach to scene or instance management.

First, objects like projectiles should generally be pooled for efficiency; plenty of info about this concept floating around.

Often the pooling concept gives you a convenient chance to store and manage both prefabs and instances. Using the Find() family of methods should always be a last resort; they're expensive.

If you don't want to get into all this right now, just create a script on your bullet prefab that self-destructs after N seconds:

Do not keep your prefab alive in the scene; it should only exist in your project's assets as a prefab.

 void Start(){
  Destroy(gameObject, DestroyTime);
 }
avatar image raulrsd · Mar 18, 2015 at 06:01 PM 0
Share

This is a nice answer. You should accept it.

avatar image EpicPandaGamer25 · Mar 18, 2015 at 07:13 PM 0
Share

I did what you said, and changed the code:

Bullet Destroy:

 #pragma strict
 
 var DestroyTime : float;
 
 function Start () {
     Destroy(gameObject, DestroyTime);
 }


Bullet Spawn:

 #pragma strict
 
 var bulletSpeed : float;
 
 function Update () {
     if (Input.Get$$anonymous$$ouseButtonUp(0)) {
         var bullet = Instantiate(gameObject,GameObject.Find("BulletSpawn").transform.position,Quaternion.identity);
         bullet.rigidbody.AddForce(transform.forward * 2000);
 
     }
 }

I moved both the bullet spawn and bullet destroy scripts to the prefab, after deleting the prefab from the scene. Now the Projectiles do not spawn at all. Once I moved the scripts, i changed the destroy time to 7 seconds and the speed to 2000, but it still doesn't spawn them. What do I do to fix this?

avatar image EpicPandaGamer25 · Mar 18, 2015 at 07:16 PM 0
Share

Also, if the above comment cannot be fixed for some reason, what is "pooling" like you mentioned earlier?

This entire question was asked because of this youtube series: http://adf.ly/1AO5Cm (episode 5 is where i encountered the problem)

avatar image AlwaysSunny · Mar 18, 2015 at 07:42 PM 0
Share

You should spend some time looking over Unity's on-site tutorials and lessons. A bullet should not spawn itself, nor should it need to Find() the location from which to spawn. If this approach was suggested by a youtube video, I wouldn't recommend following their advice.

Ins$$anonymous$$d, some other object (like a Player or a Gun) should handle spawning by keeping a reference to the bullet prefab.

 // on your gun/player/turret/whatever script, assign the prefab by click-n-dragging it from your assets folder
 public GameObject bulletPrefab;

Pooling is a concept used to reduce the cost of spawning objects at runtime. There's a lot written on the subject, so finding more information should be easy. Search for "Unity Object Pooling" or something. Don't worry about this yet though; learn your fundamentals first.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by TSRajesh · Mar 19, 2015 at 03:59 AM

I use Invoke for my arrows.. Thing is, i don't want to delete them if it sticks somewhere.. but self-destroy if didn't hit anywhere.. Works OK for me..

 public class Arrow : MonoBehaviour {
 
     void Start()
     {
         Invoke("DestroyMe", 10);
     }
 
     void OnTriggerEnter(Collider other) {
         CancelInvoke("DestroyMe");
         //Blah Blah
     }
 
     void DestroyMe()
     {
         Destroy(transform.gameObject);
     }
 
 }
Comment
Add comment · Show 8 · 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 EpicPandaGamer25 · Mar 19, 2015 at 12:56 PM 0
Share

ok, and what script do you use to spawn them? where do you attach this script too?

avatar image SingularDeviation · Mar 19, 2015 at 01:10 PM 0
Share

I would say the script is a component of the arrows themselves, considering the name of the class and the fact that Destroy() is called on the gameobject the script is attached to.

avatar image EpicPandaGamer25 · Mar 19, 2015 at 09:33 PM 0
Share

ok, and what script do you use to spawn them?

avatar image TSRajesh · Mar 19, 2015 at 10:02 PM 0
Share

Yes, this script is attached to the arrow.. In the prefab. I spawn it with a simple "instantiate" command.. The usual stuff when you create an object from a prefab.. I do that in my bow script, when my "fire" key is pressed..

avatar image EpicPandaGamer25 · Mar 19, 2015 at 10:34 PM 0
Share
 var arrowobject : GameObject;
 
 arrow = Instantiate(arrowobject, gameObject.transform.position, Quaternion.identity);

Is that similar to the code that you use?

Show more comments
avatar image
0

Answer by Fanttum · Mar 20, 2015 at 01:38 AM

Watch this!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to instantiate a Prefab ? 2 Answers

[Urgent]Changing variables of an object AFTER instantiation 2 Answers

Instantiate prefabs just before it comes into view 2 Answers

How to instantiate multiple prefabs in multiple positions? 0 Answers

Instantiate prefab within parameters?(Javascript) 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