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 /
This question was closed Apr 25, 2018 at 11:52 PM by S_jay1 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by S_jay1 · Jan 26, 2018 at 12:55 AM · destroymissingreferenceexception

How do I disable the gameobject by script?

Currently, I have Destroy(gameObject) within a OnTriggerEnter function. However, because of this, the player is destroyed and I get this 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." How would I just disable it instead of destroying it? Or how would I check if it is null?

This is my destroy player code:
void OnTriggerEnter (Collider other) { if (other.gameObject.tag == "Floor") { return; }

         if (other.gameObject.tag != "Floor") {
             Destroy (gameObject);
             PlayerisDead ();
 
     
         }
 }

This is where I get the error:

 void Update()
     {
         if(player.transform.position.z > distance) //error here
         {
             InstantiateObject();
             spawnDistance = player.transform.position.z + 70.0f;
             distance = spawnDistance - 30.0f; 
 
 
         }
         if (player.transform.position.z > updateScoreDistance)
         {
             updateScoreDistance += 10; 
             score += 1;
             scoreText.text = "" + score;
         }
 
     
     }
     
 }
 

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

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by KittenSnipes · Jan 26, 2018 at 01:28 AM

@S_jay1

  void Update()
          {
          if (player != null) {
              if(player.transform.position.z > distance) 
              {
                  InstantiateObject();
                  spawnDistance = player.transform.position.z + 70.0f;
                  distance = spawnDistance - 30.0f; 
      
      
              }
              if (player.transform.position.z > updateScoreDistance)
              {
                  updateScoreDistance += 10; 
                  score += 1;
                  scoreText.text = "" + score;
              }  
              }
          }
      }

If you don’t want to Destroy the gameObject you can use this instead:

 gameObject.SetSctive(false);





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 S_jay1 · Jan 26, 2018 at 08:11 PM 0
Share

The if (player != null) worked! Thank you so much!

avatar image calpolican S_jay1 · Jan 26, 2018 at 08:44 PM 0
Share

you should accept the answer then ;)

avatar image
0

Answer by Ginxx009 · Jan 26, 2018 at 01:24 AM

Instead of Destroying it why don't you just do it something like enable and disable.

From here :

 if (other.gameObject.tag != "Floor") {
              Destroy (gameObject);
              PlayerisDead ();
          }

To this :

 if (other.gameObject.tag != "Floor") {
              gameObject.setActive(false);
              PlayerisDead ();        
          }

Hope it helps

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 S_jay1 · Jan 26, 2018 at 08:10 PM 0
Share

It gives me this error UnityEngine.GameObject does not contain a definition for setActive and no extension method setActive accepting a first argument of type UnityEngine.gameobject could be found.

Follow this Question

Answers Answers and Comments

79 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

Related Questions

MissingReferenceException 1 Answer

Pass a copy of a GameObject as variable to another script? 1 Answer

Remove dead enemies from array 1 Answer

MissingReferenceException: The object of type 'Transform' has been destroyed but you are still trying to access it. 2 Answers

Destroying and Loading Object Promblem? 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