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 /
  • Help Room /
avatar image
0
Question by ComradeVanti · Mar 19, 2016 at 05:18 PM · gameobjectdestroynull

Gameobject is null for no reason

So here is my problem:

I have a Gameobject with a script attached. Some other script calles this script.

Now here is the deal:

Sometimes the object and thus also the script are null. This wouldnt be so strange if i couldnt see gameobject and script in the hirarchy while a debug tells me that they are null...

I made a debug that debugs the object every frame and... weird thing... Its null every second frame, but present in the frames between...

Really dont know whats going on here...

Anyone got any help?

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 Zeoli · Mar 20, 2016 at 07:22 AM 0
Share

Can you post some code? The only thing i can think of right now is that something else is destroying it on the 2nd frame

avatar image Ali-hatem · Mar 20, 2016 at 04:45 PM 0
Share

if you don't call the script in a proper way you will get null it have nothing to do with game object if it active in hirarchy or not so show us your code how do you call this script

avatar image ComradeVanti · Mar 24, 2016 at 12:02 PM 0
Share

So here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class CardLine : $$anonymous$$onoBehaviour
 {
 
     private SpriteRenderer myRenderer;
 
     void Awake ()
     {
         myRenderer = GetComponent<SpriteRenderer> ();
     }
 
     public void initialize ()
     {
         name = transform.parent.name + "_line";
         deactivate ();
     }
 
     public void activate (Color color)
     {
         myRenderer.enabled = true;
         myRenderer.color = color;
     }
 
     public void deactivate ()
     {
         if (this) {
             myRenderer = GetComponent<SpriteRenderer> ();
             if (myRenderer.enabled) {
                 myRenderer.color = Color.white;
                 myRenderer.enabled = false;
             }
         }
     }
 }
 

I use this script to outline cards in a card game. Before I activate the card I deactivate all of them --> deactivate() gets called. I know this works, as a debug in this method gets activated. But when I try to turn off the spriterenderer I get the message that it has been destroyed. So I added an if to check if it was there. But now I got an error that the object is destroyed. So I added an if to check if "this" is present but as I said its null on every seconds frame :(

Pls help :)

avatar image Ali-hatem ComradeVanti · Mar 24, 2016 at 12:19 PM 0
Share

so provide the error log & the other script you use to call this script functions & to which objects thees scripts is attached to i am afraid you try disable SpriteRenderer of thees objects which they might not have .

avatar image ComradeVanti Ali-hatem · Mar 25, 2016 at 05:11 PM 0
Share

So i dont want to spam you with screenshots here...

  1. Just trust me that the object has a spriterenderer

  2. Why do you need the script that calls this method? Its not important....

  3. The error is: $$anonymous$$issingReferenceException: The object of type 'SpriteRenderer' 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.

Please note that this error sometimes doesnt say that my spriterenderer is null but that the whole gameobject its attached to is destroyed.

Show more comments
avatar image ComradeVanti · Mar 25, 2016 at 09:57 PM 0
Share

Ali hatem

Cant add more comments.

So if i got this correctly you think that the script is trying to disable the spriterender of the object that calls deactivate(). I checked that.

So I think you are wrong here. I placed a Debug.Log(gameobject.name) in deactivate() and it debugs the correct name of the object that has the sprite renderer.

avatar image Ali-hatem ComradeVanti · Mar 25, 2016 at 10:33 PM 0
Share

you are rigte i tested it my self sorry abot that.

avatar image ComradeVanti Ali-hatem · Mar 25, 2016 at 10:44 PM 0
Share

Ali hatem

No problem. I have done some more testing. The error now appears on the line of the debug. So it debug fine for most objects but there is always one or mabye two (or mabye more... it seams random) gameobject that cant even debug themselves because they have been destroyed.

But I am now sure that no gameobject is actually destroyed.

What i have done is:

 void Update ()
     {
         Debug.Log (FindObjectsOfType<CardLine> ().Length);
     }

to check if any objects actually get destroyed. But the number remains constant, even after the error. Also I have now stopped enabeling and disabeling the renderer. Ins$$anonymous$$d I just set it to an invisible color. But I still get the error...

I am at the end here xD

1 Reply

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

Answer by ComradeVanti · Mar 28, 2016 at 09:25 PM

Hey guys. So I have figured out the problem... So I will just answer my onw question here xD

So: Problem was that something with my Lists wrent wrong. You know I have a List of CardLines and I deactivated them... so for some reason each CardLine existed 2 times but the second clone or what it was, was some kind of virtual element that didnt acctually exist... so that was the problem :)

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

61 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

Related Questions

Destroyed instance of Prefab, can't spawn it back. 1 Answer

I can't destroy my instantiated game object 1 Answer

Destroy(GameObject) works after 3 attempts 0 Answers

NullRefrenceException: Object refrence not set to an instance of an object. 0 Answers

Collision detection in 2017 3d 4.2f2 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