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 /
  • Help Room /
avatar image
0
Question by CrispGalileo · Jul 26, 2021 at 02:59 PM · scenenullreferenceexceptionscene-loadingdictionary

Trouble with SceneMananger.sceneLoaded and passing dictionaries

Pardon the very ugly code. If I can get this to work I will try to go back and make it more readable and expandable.

Basically I am trying to create a dictionary containing an id number and a gameObject for each of my different collectible types upon loading a level (What GenerateItemDictionaries() does here).

Then, upon picking up an item, I want to remove it from the dictionary and a separate list of id's that I'm using to save the level data elsewhere.

The problem is that it seems like immediately after I run GenerateItemDictionaries(), where honeyDict is created, my OnItemPickup() function gets honeyDict == null.

This is run in a separate class using SceneManager.sceneLoaded += OnSceneLoad;

     private void OnSceneLoad(Scene scene, LoadSceneMode mode)
     {
         levelDataListManager.hasGeneratedItemDictionaries = false;
         GenerateUI();
         ResetPlayerProperites();
         SaveOnSceneLoad();
         levelDataListManager.GenerateItemDictionarys();
         levelDataListManager.GenerateLevel();
     }

These are both in my LevelDataListManager class.

     public void GenerateItemDictionarys()
     {
         Debug.Log("Generating Dictionaries");
         honeyDict = new Dictionary<int, GameObject>();
         int i = 0;
         foreach (GameObject honeyObject in GameObject.FindGameObjectsWithTag(itemTags[0]))
         {
             honeyDict.Add(i, honeyObject);
             i++;
         }
 
         keyDict = new Dictionary<int, GameObject>();
         int j = 0;
         foreach (GameObject keyObject in GameObject.FindGameObjectsWithTag(itemTags[1]))
         {
             keyDict.Add(j, keyObject);
             j++;
         }
         if (honeyDict == null)
         {
             Debug.Log("honeyDict null in GenerateItemDictionaries");
         }
         hasGeneratedItemDictionaries = true;
     }


   public void OnItemPickup(int itemTypeIDNum, GameObject item)
     {
         switch (itemTypeIDNum) 
         {
             case 0:
                 if (!hasGeneratedItemDictionaries)
                 {
                     Debug.Log("hasn't generated the dicitonaries dingus");
                 }
                 if (honeyDict == null)
                 {
                     Debug.Log("honeyDict null in switch");
                 }
                 int honeyKey = FindGameObjectKey(item, honeyDict);
                 honeyDict.Remove(honeyKey);
                 currentLevel.honeyArray.Remove(honeyKey);
                 break;
             case 1:
                 int keyKey = FindGameObjectKey(item, keyDict);
                 keyDict.Remove(keyKey);
                 currentLevel.keyArray.Remove(keyKey);
                 break;
         }
 
     }

 

The only places where the value of hasGeneratedItemDictionaries are changed are in GenerateItemDictionaries() and inside OnSceneLoad().

So, if I go by the order things run inside OnSceneLoad(), hasGeneratedItemDictionaries is set to false, and then after OnSceneLoad() runs GenerateItemDictionaries(), hasGeneratedItemDictionaries is set to true. It couldn't be set back to false until another scene loads, and GenerateItemDictionaries() is only called inside OnSceneLoad(). So I am very confused as to how GenerateItemDictionaries() sets hasGeneratedItemDictionaries to true, and then without any scene load in between it is back to false.

alt text

If I toss this in:

     void Update()
     {
         if (!hasGeneratedItemDictionaries)
         {
             Debug.Log("!hasGeneratedItemDictionaries");
         }
     }

It does not trigger at any point. I am not running OnItemPickup on the same frame the scene loads, either. That triggers after several frames of me walking over to a collectable and hitting it's trigger. I have been working at this one for hours and my eyes are starting to glaze over any code. Any help is very much appreciated!!

dingus-error.png (36.7 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

197 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 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

Multiple active scenes updating but not rendering? 0 Answers

Getting "object reference not set" error when creating a list dictionary. 1 Answer

Can you help me to find a script for collision a sphere to an object and change the scenes in the same time ? 1 Answer

DontDestroyOnLoad wont let me play game 0 Answers

NullReferenceException: A null value was found where an object instance was required. at InitScript.LoginCallback2 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