Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 jimm84 · May 22, 2020 at 09:44 AM · gameobjectinstantiateprefabdestroybullet

Issues with destroying a game object

Hello all, I finding that one aspect of my game is fighting me every single step of the way and any help here would be appreciated. I have a bit understanding of C# but I'm not a guru by any stretch.

So here is the issue, I am following an online tutorial to instantiate bullet, or in my case a blast the shoots out from the player. So far so good on getting the blast to spawn from the middle of the player but then comes the challenge. The blast stays where I leave it or press the space bar - forevermore radiating out. This would be great for dropping mines or decoys but that isn't my intent. The blast needs to happen, disappear, and reappear when space is pressed again etc.

I'm getting a warning saying that : "Destroying assets is not permitted to avoid data loss. If you really want to remove an asset use DestroyImmediate (theObject, true); UnityEngine.Object:Destroy(Object, Single) PlayerAtk:fire() (at Assets/Scripts2019/PlayerAtk.cs:35) PlayerAtk:Update() (at Assets/Scripts2019/PlayerAtk.cs:24)"

I have also tried : Destroy(this, 2f); //it just kills the script I have also tried : Destroy(EMP2, 2f); //says I can't delete and asset I have also tried : Destroy(gameObject, 2f); //says I can't due to a null object

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerAtk : MonoBehaviour {
 
     // Use this for initialization
     public GameObject EMP2; //= player // Calls Emp Prefab
     Vector2 blastPos; // variable for where  to put he blastpos
     public float fireRate = 0.5f;
     float nextFire = 0.0f;
 
     void Start (){
         // perhaps find picks ups to enable limited number of blasts
     }
 
     void Update()
     {   
 
         if(Input.GetKeyDown("space") && Time.time > nextFire)
             
         {
             nextFire = Time.time + fireRate;
             fire (); 
             // when space is pressed, this will call blast wave - "void fire" and all that happens
         }
     }
 
     void fire()
     {
         Debug.Log("blam!");
         blastPos = transform.position;
         blastPos += new Vector2(+0f, 0f);
         Instantiate (EMP2, blastPos, Quaternion.identity);
         Destroy (gameObject, 2f);
 
 
         //https://www.youtube.com/watch?v=1QOsUrXWMWY
         //7.30 blast creates, is solid, needs to vanish, rigid body, 
 
         // elements of what "Attack" does eg. sends rigid bodies flying, 
         // enemies flying in a radius around the player.  
         // enemies only
 
     }
 }

Thanks for your help, tips, and comments!

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 myzzie · May 22, 2020 at 10:05 AM 0
Share

You should read up on how Instantiate works. Specifically

Returns

Object The instantiated clone.

Description

Clones the object original and returns the clone.

avatar image jimm84 myzzie · May 22, 2020 at 03:09 PM 0
Share

Thank you I shall have a look. The tutorial I was following didn't need to add much here.

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Master_Zen · May 22, 2020 at 10:58 AM

You need to destroy the object you instantiated. For this you need to keep a refference. Just change your Fire method to somethink like this:

 void fire()
      {
          Debug.Log("blam!");
          blastPos = transform.position;
          blastPos += new Vector2(+0f, 0f);
          GameObject refference = Instantiate (EMP2, blastPos, Quaternion.identity);
          Destroy (refference, 2f);
      }
Comment
Add comment · Show 4 · 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 jimm84 · May 22, 2020 at 03:08 PM 0
Share

Thank you, that seems to have done the trick! Could I can see that the "refference" has allowed it to work correctly but why is this the case when the tutorial I followed didn't have to do this?

 void fire()
     {
         Debug.Log("blam!");
         blastPos = transform.position;
         blastPos += new Vector2(+0f, 0f);
         //Instantiate (E$$anonymous$$P2, blastPos, Quaternion.identity);
         //Destroy (gameObject, 2f);
         GameObject refference = Instantiate (E$$anonymous$$P2, blastPos, Quaternion.identity);
         Destroy (refference, 0.3f);
         
 
     }
avatar image Master_Zen jimm84 · May 22, 2020 at 04:15 PM 1
Share

You surely have made some changes because the code of the fire method itself should not work, BUT if you just look at this line: Destroy (gameObject, 2f);this can work if the script that contains this line sits on the bullet (or the gameobject that needs to destroyed again). The gameObject is a pointer to the real Object sitting in your scene, after you Instantiated it. It comes from $$anonymous$$onoBehavior the class all GameObjects inherit from.

avatar image jimm84 Master_Zen · May 22, 2020 at 05:16 PM 0
Share

Ah, I wonder if I had attached this the 'blast' and not the player it may have been different. Thinking back to it. Thank you for your help!

avatar image Master_Zen jimm84 · May 22, 2020 at 04:17 PM 0
Share

And when i say it should not work, i mean not in the intended way. You set the position of the bullet to transform.position, which is the position of the object the script is attached to. And if you call this method on a bullet, then it will always spawn at the same position. To be exact, the position the prefab has.

avatar image
1

Answer by GoGoLoN · May 22, 2020 at 11:03 AM

Do you want to destroy the object you instantiate or the one that the script is attached to? I guess this is what you want to do:

 GameObject blastObj = Instantiate (EMP2,blastPos, Quaternion.identity) as GameObject;  
 Destroy(blastObj, 2f);




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 jimm84 · May 22, 2020 at 03:08 PM 0
Share

Thanks for the answer!

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

240 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 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 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 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 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 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 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 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 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 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 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 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

How many time does a prefab takes to load?? 1 Answer

Deleting a GameObject 2 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

PlayerRespawn class wont Instantiate the player prefab 1 Answer

Cannot destroy child object in prefab- Error 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