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 NxT01 · May 02 at 05:55 PM · missingreferenceexception

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

I am making a 3d game and i made it so whenever the player collides with a object tagged "walls". he gets turned off and the camera, you lose,text, and retry button get activated. but something kinda broke and whenever i die and click the retry button the error comes and it wont let me retry. what is supposed to happen is that i get teleported back to spawn and the other things get turned off again. im new to coding so idk what this is.

here is the error:---------------------------------------------------

MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. button.Update () (at Assets/Scripts/button.cs:23)

here is the button script:-----------------------------------------

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class button : MonoBehaviour
 {
     public GameObject player;
     public GameObject deadcam;
     public GameObject deadtext;
 
 
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         if(Input.GetMouseButtonDown(0))
         {
             player.transform.position = new Vector3(78.3f, 1.55f, 0f);
 
             player.SetActive(true);
 
             deadcam.SetActive(false);
 
             deadtext.SetActive(false);
 
             gameObject.SetActive(false);
         }
     }
 }

and here is the player script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class movment : MonoBehaviour
 {
     float xInput;
     float zInput;
 
     public float speed;
 
     Rigidbody rb;
 
     public GameObject winText;
 
     public GameObject deadCam;
     public GameObject deadText;
     public GameObject button;
 
 
 
 
     // Start is called before the first frame update
     void Start()
     {
         rb = GetComponent<Rigidbody>();
     }
 
     // Update is called once per frame
     void Update()
     {
 
         xInput = Input.GetAxis("Horizontal");
         zInput = Input.GetAxis("Vertical");
 
         rb.AddForce(xInput * speed,0,zInput * speed);
 
         if(Input.GetKeyDown(KeyCode.Space))
         {
             rb.AddForce(Vector3.up * 200);
         }
 
        
             }
         void OnCollisionEnter(Collision collision)
         {
             if(collision.gameObject.tag == "Walls")
             {
                 Destroy(gameObject);
 
                 gameObject.SetActive(false);
 
 
                 deadCam.SetActive(true);
                 deadText.SetActive(true);
                 button.SetActive(true);
         }
         
             if(collision.gameObject.tag == "Win")
             {
                 Destroy(collision.gameObject);
 
                 winText.SetActive(true);
 
                
 
                 //SceneManager.LoadScene("level2");
         }
 
 
         }
        
 }





ok turns out it was the dumbest thing ever. in my player script i said that if game object collides with an object tagged wall he will get "DESTROYED" but then if i click retry its supposed to set him active. the wrong thing was that i said to "destroy" him, i was supposed to set his active to false" and not destroy him. now its working perfectly so if you have this problem check if you are destroying something and then setting it active instead of setting it to false. and dont give up on your project hope this helps someone. this was kinda just a dumb mistake of me but yeah.

Comment
Add comment
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

1 Reply

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

Answer by NxT01 · May 03 at 07:28 PM

explained it already. works for me

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

136 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

Related Questions

Trying to access destroyed object 1 Answer

Why does my Bullet prefab go missing from my gun script when destroyed? 1 Answer

Texture2D variable causes MissingReferenceException 0 Answers

How can throw "Missing reference exception" this gameObject?.Getcomponent<>() ?? 2 Answers

Keep GameObject to variable linkage on LoadLevel 0 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