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 flaxibitz · May 03, 2021 at 01:12 PM · scripting problemnullreferenceexceptionawakeobject-reference-error

I have a SO in my quiz game. When i reference it in the inspector unity freezes. But when i dont, it works without loading the questions and throws nullreferenceexception error

using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Linq; using TMPro; using UnityEngine.SceneManagement;

public class QueManager : MonoBehaviour { #region Variables

 private Data data = new Data();
 
 [SerializeField] GameEvents events = null;

 [SerializeField] Animator timerAnimtor = null;
 [SerializeField] TextMeshProUGUI timerText = null;
 [SerializeField] Color timerHalfWayOutColor = Color.yellow;
 [SerializeField] Color timerAlmostOutColor = Color.red;
 private Color timerDefaultColor = Color.white;

 private List<AnswerData> PickedAnswers = new List<AnswerData>();
 private List<int> FinishedQuestions = new List<int>();
 private int currentQuestion = 0;
 

 private int timerStateParaHash = 0;
 private IEnumerator IE_WaitTillNextRound = null;
 private IEnumerator IE_StartTimer = null;

 private bool IsFinished
 {
     get
     {
         return (FinishedQuestions.Count < data.Questions.Length) ? false : true;
     }
 }

 #endregion

 #region Default Unity Methods
 void OnEnable()
 {
     events.UpdateQuestionAnswer += UpdateAnswers;
 }

 void OnDisable()
 {
     **events.UpdateQuestionAnswer -= UpdateAnswers;** This is the troublesome part
 }

 void Awake()
 {
     **events.CurrentFinalScore = 0;** This is the troublesome part
 }

 void Start ()
 {
     events.StartupHighscore = PlayerPrefs.GetInt(GameUtility.SavePrefKey);

     timerDefaultColor = timerText.color;
     LoadData();

     timerStateParaHash = Animator.StringToHash("TimerState");

     var seed = UnityEngine.Random.Range(int.MinValue, int.MaxValue);
     UnityEngine.Random.InitState(seed);

     Display();
 }
 #endregion

Below is the scriptable object

using System.Collections; using System.Collections.Generic; using UnityEngine;

[CreateAssetMenu(fileName = "GameEvents", menuName = "Quiz/new GameEvents")] public class GameEvents : ScriptableObject { public delegate void UpdateQuestionUICallback(Question question); public UpdateQuestionUICallback UpdateQuestionUI;

public delegate void UpdateQuestionAnswerCallback(AnswerData pickedAnswer); public UpdateQuestionAnswerCallback UpdateQuestionAnswer;

public delegate void DisplayResolutionScreenCallback(UIManager.ResolutionScreenType type, int score); public DisplayResolutionScreenCallback DisplayResolutionScreen;

public delegate void ScoreUpdateCallback(); public ScoreUpdateCallback ScoreUpdated;

 [HideInInspector]
 public int CurrentFinalScore;

 [HideInInspector]
 public int StartupHighscore;

}

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 oscarAbraham · May 04, 2021 at 04:47 AM 0
Share

Hi, can you fix the formating for the ScriptableObject? I'm not sure I'm reading it properly.

That said, It doesn't seem like the problem is in that code. Unity freezes are almost always the result of an infinite loop. It's not hard to cause infinite loops when using multiple delegates that can be accessed from all kinds of different places. I bet that's the cause of your problems: a delegate call results in a chain of methods that calls that delegate again. When you remove the ScriptableObject, the delegates can't be assigned, so the infinite loop is avoided.

To debug it, it can be useful to access the log file: Right click on the Console tab ( the part that says "Console"), click "Open Editor Log", open it with NotePad, and click "Save As..." to find where the log is saved in your computer. Now you can read the log even if the editor froze. The next steps are to put some Debug.Logs in the code that executes those Delegates and in the methods that get added to those Delegates, cause the freeze to happen, force-close Unity, and Open the Log file to get an idea of what Delegates are part of the problem.

Additionally, if you share the delegate calling code and the listener methods, we might be able to help you find what is causing the infinite loop.

avatar image flaxibitz · May 07, 2021 at 08:53 AM 0
Share

@oscarAbraham i tried what you said about the log files. It displayed the same concerns as the console without any helpful points...

avatar image oscarAbraham flaxibitz · May 10, 2021 at 02:11 PM 0
Share

It's expected to be the same. The point it's that you can't normally see the logs while Unity is frozen; now you can put some Debug.Log("text"); in your delegates to see in the logs what it's the order they are called in and which ones are involved in the freezing. Alternatively, if you know how to use your IDE's debugger, you could put some stops to manually step through your callbacks and find that information.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by flaxibitz · May 07, 2021 at 10:04 PM

Thank you very much @oscarAbraham. below is the code where the delegates were created:

 using System.Collections; 
 using System.Collections.Generic; 
 using UnityEngine;
 
 [CreateAssetMenu(fileName = "GameEvents", menuName = "Quiz/new GameEvents")] 
 
 public class GameEvents : ScriptableObject { 
 
 public delegate void UpdateQuestionUICallback(Question question); 
 public UpdateQuestionUICallback UpdateQuestionUI;
 
 public delegate void UpdateQuestionAnswerCallback(AnswerData pickedAnswer); 
 public UpdateQuestionAnswerCallback UpdateQuestionAnswer;
 
 public delegate void DisplayResolutionScreenCallback(UIManager.ResolutionScreenType type, int score); 
 public DisplayResolutionScreenCallback DisplayResolutionScreen;
 
 public delegate void ScoreUpdateCallback(); 
 public ScoreUpdateCallback ScoreUpdated;
 
 [HideInInspector]
  public int CurrentFinalScore;
  [HideInInspector]
  public int StartupHighscore;

Here is where the problematic code is:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Linq;
 using TMPro;
 using UnityEngine.SceneManagement;
 
 public class QueManager : MonoBehaviour
 {
     #region Variables
     
     private Data data = new Data();
     
     [SerializeField] GameEvents events = null;
 
     [SerializeField] Animator timerAnimtor = null;
     [SerializeField] TextMeshProUGUI timerText = null;
     [SerializeField] Color timerHalfWayOutColor = Color.yellow;
     [SerializeField] Color timerAlmostOutColor = Color.red;
     private Color timerDefaultColor = Color.white;
 
     private List<AnswerData> PickedAnswers = new List<AnswerData>();
     private List<int> FinishedQuestions = new List<int>();
     private int currentQuestion = 0;
     
 
     private int timerStateParaHash = 0;
     private IEnumerator IE_WaitTillNextRound = null;
     private IEnumerator IE_StartTimer = null;
 
     private bool IsFinished
     {
         get
         {
             return (FinishedQuestions.Count < data.Questions.Length) ? false : true;
         }
     }
 
     #endregion
 
     #region Default Unity Methods
     void OnEnable()
     {
         events.UpdateQuestionAnswer += UpdateAnswers;
     }
 
     void OnDisable()
     {
         events.UpdateQuestionAnswer -= UpdateAnswers; //This Line has Issues...
     }
 
     void Awake()
     {
         events.CurrentFinalScore = 0; //This Line has Issues...
     }
 
     void Start ()
     {
         events.StartupHighscore = PlayerPrefs.GetInt(GameUtility.SavePrefKey);
 
         timerDefaultColor = timerText.color;
         LoadData();
 
         timerStateParaHash = Animator.StringToHash("TimerState");
 
         var seed = UnityEngine.Random.Range(int.MinValue, int.MaxValue);
         UnityEngine.Random.InitState(seed);
 
         Display();
     }
     #endregion

     public void UpdateAnswers(AnswerData newAnswer)
     {
         if(data.Questions[currentQuestion].Type == AnswerType.Single)
         {
             foreach (var answer in PickedAnswers)
             {
                 if(answer != newAnswer)
                 {
                     answer.Reset();
                 }
                 PickedAnswers.Clear();
                 PickedAnswers.Add(newAnswer);
             }
         }
         else
         {
             bool alreadyPicked = PickedAnswers.Exists(x => x == newAnswer);
             if(alreadyPicked)
             {
                 PickedAnswers.Add(newAnswer);
             }
             else
             {
                 PickedAnswers.Add(newAnswer);
             }
         }
     }
 
     public void EraseAnswers ()
     {
         PickedAnswers = new List<AnswerData>();
     }
 
     void Display()
     {
         EraseAnswers();
         var question = GetRandomQuestion();
 
         if (events.UpdateQuestionUI != null)
         {
             events.UpdateQuestionUI(question);
         }
 
         /*if(question.UseTimer)
         {
             UpdateTimer(question.UseTimer);
         }*/
     }
 
     public void Accept()
     {
         UpdateTimer(false);
         bool IsCorrect = CheckAnswers();
         FinishedQuestions.Add(currentQuestion);
 
         UpdateScore((IsCorrect) ? data.Questions[currentQuestion].AddScore : -data.Questions[currentQuestion].AddScore);
 
         if(IsFinished)
         {
             SetHighscore();
         }
 
         var type = (IsFinished) ? UIManager.ResolutionScreenType.Finish : (IsCorrect) ? UIManager.ResolutionScreenType.Correct : UIManager.ResolutionScreenType.Incorrect;
 
         if (events.DisplayResolutionScreen != null)
         {
             events.DisplayResolutionScreen(type, data.Questions[currentQuestion].AddScore);
         }
         if(type != UIManager.ResolutionScreenType.Finish){
             if (IE_WaitTillNextRound != null)
             {
                 StopCoroutine(IE_WaitTillNextRound);
             }
             IE_WaitTillNextRound = WaitTillNextRound();
             StartCoroutine(IE_WaitTillNextRound);
         }
     }
 
     #region Timer Methods
     void UpdateTimer (bool state)
     {
         switch(state)
         {
             case true:
                 IE_StartTimer = StartTimer();
                 StartCoroutine(IE_StartTimer);
 
                 timerAnimtor.SetInteger(timerStateParaHash, 2);
                 break;
             case false:
                 if(IE_StartTimer != null)
                 {
                     StopCoroutine(IE_StartTimer);
                 }
 
                 timerAnimtor.SetInteger(timerStateParaHash, 1);
                 break;
         }
     }
 
     IEnumerator StartTimer()
     {
         var totalTime = data.Questions[currentQuestion].Timer;
         var timeLeft = totalTime;
 
         timerText.color = timerDefaultColor;
         while(timeLeft > 0)
         {
             timeLeft--;
 
             if(timeLeft < totalTime / 2 && timeLeft > totalTime /4)
             {
                 timerText.color = timerHalfWayOutColor;
             }
             if(timeLeft < totalTime / 4)
             {
                 timerText.color = timerAlmostOutColor;
             }
 
 
             timerText.text = timeLeft.ToString();
             yield return new WaitForSeconds(1.0f);
         }
         Accept();
     }
 
     IEnumerator WaitTillNextRound()
     {
         yield return new WaitForSeconds(GameUtility.ResolutionDelayTime);
         Display();
     }
     #endregion
 
     /*Question GetRandomQuestion ()
     {
         var randomIndex = GetRandomQuestionIndex();
         currentQuestion = randomIndex;
 
         return data.Questions[currentQuestion];
     }
 
     int GetRandomQuestionIndex ()
     {
         var random = 0;
         if (FinishedQuestions.Count < data.Questions.Length)
         {
             do
             {
                 random = UnityEngine.Random.Range(0, data.Questions.Length);
             }while (FinishedQuestions.Contains(random) || random == currentQuestion);
         }
         return random;
     }*/
 
     bool CheckAnswers()
     {
         if(!CompareAnswers())
         {
             return false;
         }
         return true;
     }
 
     bool CompareAnswers()
     {
         if(PickedAnswers.Count > 0)
         {
             List<int> c = data.Questions[currentQuestion].GetCorrectAnswers();
             List<int> p = PickedAnswers.Select(x => x.AnswerIndex).ToList();
 
             var f = c.Except(p).ToList();
             var s = p.Except(c).ToList();
 
             return !f.Any() && !s.Any();
         }
         return false;
     }
 //Function called to load data(Questions)..
     void LoadData()
     {
        data = Data.Fetch();
     }
 
     public void RestartGame()
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
     }
 
     public void QuitQuiz()
     {
         SceneManager.LoadScene("QuizHome");
     }
 
     private void SetHighscore()
     {
         var highscore = PlayerPrefs.GetInt(GameUtility.SavePrefKey);
         if(highscore < events.CurrentFinalScore)
         {
             PlayerPrefs.SetInt(GameUtility.SavePrefKey, events.CurrentFinalScore);
         }
     }
 
     private void UpdateScore(int add)
     {
         events.CurrentFinalScore += add;
 
         if(events.CurrentFinalScore < 0)
         {
             events.CurrentFinalScore = 0;
         }
         events.ScoreUpdated ?.Invoke();
     }
 
     #region  Getters
 
     Question GetRandomQuestion()
     {
         var randomIndex = GetRandomQuestionIndex();
         currentQuestion = randomIndex;
 
         return data.Questions[currentQuestion];
     }
     int GetRandomQuestionIndex()
     {
         var random = 0;
         if(FinishedQuestions.Count < data.Questions.Length)
         {
             do
             {
                 random = UnityEngine.Random.Range(0, data.Questions.Length);
             }
             while(FinishedQuestions.Contains(random) || random == currentQuestion);
         }
         return random;
     }
 
     #endregion
 }
 



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 oscarAbraham · May 06, 2021 at 10:50 PM 0
Share

Hi. You shouldn't post this as an answer; this should be an edit to your question, or at least a comment. Anyway, this is not the code we need to see. We need to see the code that calls the delegates; the code where UpdateQuestionAnswer(); is written, and the same for the other delegates. We also need to see the code in the listeners, (for example, the UpdateAnswers method).

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

259 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

How can I fix this error? 0 Answers

..Object reference not set to an instance of an object.. Error occuring for a public static script variable. 1 Answer

Instantiate not working in Awake() 0 Answers

NullReferenceException in FiniteStateMachine with ThirdPersonCharacter 0 Answers

Why do i get NullReferenceException here? 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