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
0
Question by adrianjmejias · Dec 08, 2018 at 03:42 PM · bugnullreferenceexceptionincorrect

Missing references and incorrect values

So I have this really weird problem where I get incorrect values on a function called by a button and also missing references.

what is REALLY REALLYYYY WEIRD is that the variable is set on the inspector but in code the value is different, this has me really fustrated because it makes no sense

also the code is isolated within it's own namespace so there are no other scripts involved;


the problem lies in gameDatabase.actLevel that sets to null by itself (idk why or how). I tried to work around this by referencing the level by index but the indexLevel has an incorrect value when I call OprPress via button event

Data Classes

     [System.Serializable]
     public class Database
     {
         public Level[] levels;
         public Level actLevel;
     }
 
     [System.Serializable]
     public class Level
     {
         public string question;
         public string[] options;
     }

GameManager

     public class TriviaQuestions : Minigame
     {
         const int SOLUTION = 0;
         public Database gameDatabase;
         public UI gameUI;
         public Sound gameSound;
         [System.NonSerialized] public int indexLevel;
 
 
         public void OptPress(Text _pressed)
         {
             Debug.Log(JsonUtility.ToJson(gameDatabase.actLevel));
             Debug.Log("game index is  " + indexLevel);
 
             // Debug.Log(_pressed.text + "    " +gameDatabase.levels[indexLevel].options[SOLUTION]);
             MG_End(_pressed.text == gameDatabase.actLevel.options[SOLUTION]);
         }
 
         public override void MG_Start(int _id)
         {
             indexLevel = _id;
             Debug.Log("starting new game " + indexLevel);
             gameObject.SetActive(true);
             gameDatabase.actLevel = gameDatabase.levels[_id];
             gameUI.actLevel = gameDatabase.actLevel;
             Debug.Log(JsonUtility.ToJson(gameDatabase.actLevel));
         }
     }

UI Manager

 [System.Serializable]
 public class UI
 {
     [SerializeField] private Text textQuestion;
     [SerializeField] private HolderCombo.HolderCombo answerInstantiate;
     private List<GameObject> buttonOptions = new List<GameObject>();
     private List<Text> textOptions = new List<Text>();

     public Level actLevel
     {
         set
         {
             int _iLength, _vLength; // cache, evitar llamadas a count y bla bla bla
             _iLength = textOptions.Count;
             _vLength = value.options.Length;

             // Si he instanciado menos opciones de las que necesito, instanciame más
             while (_iLength < _vLength)
             {
                 GameObject go = GameObject.Instantiate(answerInstantiate.Pprefab, answerInstantiate.Pholder);
                 buttonOptions.Add(go);
                 textOptions.Add(go.GetComponentInChildren<Text>());
                 _iLength++;
             }

             string[] _opt = (string[])value.options.Clone();

             _opt.Shuffle();

             // LLenando preguntas
             for (int ii = 0; ii < _vLength; ii++)
             {
                 textOptions[ii].text = _opt[ii];
                 buttonOptions[ii].SetActive(true);
             }

             // si tengo más de las que necesito instanciadas, apagar
             for (int ii = _vLength; ii < _iLength; ii++)
             {
                 buttonOptions[ii].SetActive(false);
             }

             textQuestion.text = value.question;
         }
     }
 }



So when I run this, this is the output alt text notice the actLevel is empty when the OptPress is called but not when we start the game even when we don't update it And also, in the editor we have this alt text


Now when I use try to reference the level via index it works for the first game but the value is not updated on MB_Start call and we cant access the level we are in correctly.

So yeah I tried a lot of stuff and nothing seems to work. I started to think it's a unity bug.

capture.png (13.3 kB)
capture.png (9.4 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

107 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

Related Questions

Can I Ignore Null Reference Exceptions? ( bug? ) 3 Answers

Why am I getting NullReferenceException on my .Contains()? 1 Answer

NullReferenceException when looking at target in an Editor class (CustomEditor) 2 Answers

RequireComponent not working! 0 Answers

Unity null check 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