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
-1
Question by Thijsvt · Mar 16, 2017 at 08:32 PM · unity 5nullreferenceexception

Gameobject not null at start but in function it is.

Dear Unity,

I have a nullpointer exception and am not really sure why I am getting this. I have a script with 2 objects connected to it. Spatula and Fryingpan which I set to nonactive at the initialization of the script start as followed:

   if (RecepScenarioObjectHandelerNull())
     {
         InitializeReceptScenarioObjectHandler();
     }

  • Then it initializes like overe here

     void Start () {
         if (!IfSpatulaAndFryingPanNull())
         {
             Spatula.SetActive(false);
             FryingPan.SetActive(false);
         }
     }
    
    

This works perfectly and disabled the game objects right from the start which I want. But when I want to activate the gameobjects for use with the following function I get a nullpointer:

 public bool IfSpatulaOrFyingPanActive()
 {
     if (IfSpatulaAndFryingPanNull())
     {
         System.Diagnostics.Debug.WriteLine("One of the objects is null");
     }
     else if (Spatula.activeSelf == true || FryingPan.activeSelf == true)
     {
         return true;
     }
     return false;
 }

Even if I do not disable the objects in the beginning the debugging shows me that the objects are null and I have no clue why. I do not move to another scene or close a script.

Side note: I added the gameobjects as publics and added the objects in unity as follows:

alt text

Does anyone know what the problem could be?

Kr,

imageunity.png (11.3 kB)
Comment
Add comment · Show 3
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 Commoble · Mar 16, 2017 at 09:18 PM 0
Share

Can we see more of your code? Especially your IfSpatualAndFryingPanNull() function

avatar image jjcrawley · Mar 17, 2017 at 12:56 AM 0
Share

I concur, more code would be helpful. As it is, the only things I can recommend are putting more debugs in, simply saying that "One of the objects is null", doesn't help you figure out which one is null, or why it may be returning true. $$anonymous$$aybe it's a simple logic error. Can't say without anymore code.

avatar image Thijsvt · Mar 17, 2017 at 07:36 AM 0
Share

@Commoble and @Jicrawley to give it more context. i checked with debugging and see that the objects are not null at start. Then when i call the function with the initialized object (Which is done like this :)

 ReceptScenarioObjectHandler receptScenarioObjectHandler = = gameObject.AddComponent<ReceptScenarioObjectHandler>();

in the ReceptScenarioObjectHandler i this start method is the one that sees them active in the beginning and not null but not afterwards when i for example press a button and call a function that is supposed to activate them

THe null check code is:

 private bool IfSpatulaAndFryingPanNull()
 {
     return Spatula == null || FryingPan == null;
 }


Is this helpfull enough?

2 Replies

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

Answer by jjcrawley · Mar 17, 2017 at 02:48 PM

You're adding in another component of the same type, that's where the issue is. When you add in a new component, all the values inside that class are initialised to the default. In this case, the default values for Spatula and FryingPan are indeed null, hence your null reference exception. The one that you assigned in the editor has all of the references already sorted out, hence why they aren't null when the component starts. You need to use the gameobjects Get Component method to get the component instance, not Add Component. This instance will be the one you tweaked in the editor, the one that is on the gameobject.

Comment
Add comment · Show 3 · 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 Thijsvt · Mar 17, 2017 at 03:52 PM 0
Share

@jjcrawley you just fixed it for me and explained it perfectly can you answer it seperatly so i can mark it as answer?

avatar image jjcrawley Thijsvt · Mar 17, 2017 at 04:03 PM 0
Share

There you go. Glad that I was able to help. Best of luck with your game.

avatar image Thijsvt jjcrawley · Mar 18, 2017 at 02:25 PM 0
Share

Thank you!

avatar image
1

Answer by AnOrdinarySandwich · Mar 18, 2017 at 05:47 PM

Hi! Just a small thing, but the logic of your method for checking for nulls is named using 'and':

IfSpatulaAndFryingPanNull()

however, your code is using the 'or' test, not 'and':

return Spatula == null || FryingPan == null;

This could cause some confusion if the two aren't in agreement ;)

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 Thijsvt · Mar 21, 2017 at 08:07 AM 0
Share

Very nice will change it now thanks :)

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

NullReferenceException at line . . . 1 Answer

Im trying to build a game.I have an enemy Ai that uses the players position to track and move.But i get a null reference exception when player dies.Can anybody help ? 0 Answers

NullReference exception after StopCoroutine 2 Answers

While level restarted I get NullReferenceException 0 Answers

Has my project's data corrupted? Script keeps returning null but 20 minutes ago it wasn't? 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