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 gtagamer · Nov 11, 2014 at 10:32 PM · shooting

The object of type "gameobject" has been destroyed but you are still trying to access it

hello guys i got two problems with the script i made first its a gun shooting script and the problem is is that the bullets turn side ways when they are shot but the big problem is is that the gun only shoots like 5 times then i get this message "The object of type "gameobject" has been destroyed but you are still trying to access it " and its stops shooting does anyone know how to fix this

heres my script

 using UnityEngine;
 using System.Collections;
 
 public class Shoot : MonoBehaviour {
     
     public GameObject bullet;
     public GameObject bulletHole;
     public float delayTime = 0.5f;
     private float counter = 0;
     
     void FixedUpdate ()    
     {
         if(Input.GetKeyDown(KeyCode.Mouse0) && Time.time >= counter)
         {
             Instantiate(bullet, transform.position, transform.rotation);
             audio.Play();
             counter = Time.time + delayTime;
             
             RaycastHit hit;
             Ray ray = new Ray(transform.position, transform.forward);
             if(Physics.Raycast(ray, out hit, 100f))
             {
                 Instantiate(bulletHole, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
             }
         }
     }
 }
 
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 spiceboy9994 · Nov 11, 2014 at 11:30 PM 0
Share

Do you have an additional code where you destroy the bullet?, if this is the case, can you paste that code?

avatar image spiceboy9994 · Nov 11, 2014 at 11:32 PM 0
Share

Also, it would be better to have both bullet and bulletHole referenced to prefabs, ins$$anonymous$$d of a gameObject within the scene

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by TSRajesh · Nov 12, 2014 at 01:52 AM

  • You just instantiate.. Not giving it a forward velocity? Then it could go sideways.. most probably just fall down..

  • Where do you destroy your bullet?

  • The error message would also say which line (and file) the error happened, and that pretty much gives an idea on which "destroyed Object" you are trying to access...

I'd suggest not to post incomplete questions if you honestly want an answer.

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 shadbags · Nov 12, 2014 at 02:02 AM

I would suggest that your code for detecting if a bullet is out of bounds is destroying all bullets once the first goes out of bounds.

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 gtagamer · Nov 12, 2014 at 02:52 AM

heres my second gun script i use to eject the bullets \

 using UnityEngine;
 using System.Collections;
 
 public class MoveBullet : MonoBehaviour {
 
     public float speed = 1f;
 
     void Start ()    
      {
         Destroy(gameObject, 5f); //Delete the bullet after 5 seconds
     }
     
     void Update ()    
      {
         transform.Translate(0, 0, speed);
     }
 }
 
Comment
Add comment · Show 2 · 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 Jeff-Kesselman · Nov 12, 2014 at 04:39 AM 0
Share

Good start, now show us the complete error message including any line numbers it prints.,

avatar image spiceboy9994 · Nov 13, 2014 at 07:02 PM 0
Share

I think the error may be on the line 15. Why do you coded the Destroy on the start method?... what about if you control your game object destroy on your update with time controlled variables? $$anonymous$$aybe the error is related to update cycle running at the time that it tries to destroy the object.

 public class $$anonymous$$oveBullet: $$anonymous$$onoBehavior {
    public float speed = 1f;
    /*Total time to wait*/
    public float timeToDestroy = 5f;
    /*Total elapsed time to destroy*/
    public float elapsedTimeToDestroy;
 
    void Start()
    {
    }
 
    void Update()
    {   
        transform.Translate(0, 0, speed);
        elapsedTimeToDestroy += Time.deltaTime;
        if (elapsedTimeToDestroy >= timeToDestroy)
        {
           Destroy(gameObject);
        }
 
    }
 
 }

Give it a try and let me know if that helps

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

Multiple Cars not working 1 Answer

Gun shooting sideways. NEED HELP!!! 2 Answers

A node in a childnode? 1 Answer

why does my gun stop shooting 1 Answer

Spawning entities around player in a distance? 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