Search Unity

[Solved] Weird problem with SetActive(), possible bug?

Discussion in 'Scripting' started by whileBreak, Dec 1, 2017.

  1. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    Hi, Im having a problem when I'm trying to activate a game object. It's supposed to activate but it isn't... the first time the involved methods are called. For some reason it thinks it's activated, but it isn't. Debugging its state to the console gives me something like this. It prints true for activeSelf, but in the inspector it isn't.

    The problem is with containerCuestionary

    Code (CSharp):
    1. public void SetCuestionary(string title, Sprite icon,int id)
    2.     {
    3.         Debug.Log("Set Cuestionary");
    4.         cuestionaryIcon.sprite = icon;
    5.         cuestionaryTitle.text = title;
    6.  
    7.         containerCuestionary.SetActive(false);
    8.  
    9.         Debug.Log(containerCuestionary.activeSelf); //Prints False
    10.         containerCuestionary.SetActive(true);
    11.         Debug.Log(containerCuestionary.activeSelf); //Prints True
    12.  
    13.         cuestionaryID = id;
    14.         Debug.Log(cuestionaryID);
    15.         if(completionIcons!= null)
    16.             if(completionIcons.Length>0)
    17.                 completionIcons[cuestionaryID].color = incompleteColor;
    18.  
    19.     }
    This method is called from this other method on another GO.


    Code (CSharp):
    1. public void StartCuestionary(int i)
    2.     {
    3.         currentCuestionary = i;
    4.         currentQuestion = 0;
    5.         if (resetEachTime)
    6.             test.Cuestionaries[currentCuestionary].ResetAllQuestions();
    7.  
    8.  
    9.         testInterface.SetCuestionary(test.Cuestionaries[currentCuestionary].Title, test.Cuestionaries[currentCuestionary].Icon, currentCuestionary);
    10.  
    11.         SetInterfaceQuestion();
    12.         //Interface Init
    13. private void SetInterfaceQuestion()
    14.     {
    15.         //SORT ANSWERS RANDOM
    16.         AnswerControl[] sortedAnswers = GetCurrentSortedAnswers();
    17.  
    18.  
    19.         testInterface.SetQuestion(test.Cuestionaries[currentCuestionary].Questions[currentQuestion].Heading, sortedAnswers);
    20.     }
    21.     private AnswerControl[] GetCurrentSortedAnswers()
    22.     {
    23.         bool graphicQuestion = test.Cuestionaries[currentCuestionary].Questions[currentQuestion].GraphicQuestion;
    24.         QuestionType type = test.Cuestionaries[currentCuestionary].Questions[currentQuestion].Type;
    25.         bool isTrue = test.Cuestionaries[currentCuestionary].Questions[currentQuestion].IsTrue;
    26.         IList<AnswerModel> answers = test.Cuestionaries[currentCuestionary].Questions[currentQuestion].Answers;
    27.  
    28.         List<AnswerControl> sortedAnswers = new List<AnswerControl>(0);
    29.  
    30.         if (type == QuestionType.OneTrue)
    31.         {
    32.             List<int> answersLeft = new List<int>(0);
    33.             for (int i = 0; i < answers.Count; i++)
    34.             {
    35.                 answersLeft.Add(i);
    36.             }
    37.  
    38.             int answerID;
    39.             int rnd;
    40.  
    41.             for (int a = 0; a < answers.Count; a++)
    42.             {
    43.                 rnd = Random.Range(0, answersLeft.Count);
    44.                 answerID = answersLeft[rnd];
    45.  
    46.                 answersLeft.RemoveAt(rnd);
    47.  
    48.                 if (graphicQuestion)
    49.                     sortedAnswers.Add(new AnswerControl(this, answerID==0, answers[answerID].Sprite));
    50.                 else
    51.                     sortedAnswers.Add(new AnswerControl(this, answerID == 0, answers[answerID].Text));
    52.             }
    53.         }
    54.         else
    55.         {
    56.             if (graphicQuestion)
    57.                 sortedAnswers.Add(new AnswerControl(this, isTrue, answers[0].Sprite));
    58.             else
    59.                 sortedAnswers.Add(new AnswerControl(this, isTrue, answers[0].Text));
    60.         }
    61.  
    62.  
    63.         return sortedAnswers.ToArray();
    64.     }
    65.     }
    And it all starts here
    Code (CSharp):
    1. public override void FocusStart()
    2.     {
    3.         base.FocusStart();
    4.         img.color = focused;
    5.         TI_UIMaster.Instance.GoCuestionary();
    6.         testControl.StartCuestionary(cuestionaryID);
    7.     }
    8.  
    9. //This method is in the class TI_UIMaster
    10. public void GoCuestionary()
    11.     {
    12.  
    13.         teleportCanvas.SetActive(false);
    14.         idleCanvas.SetActive(false);
    15.         cuestionaryCanvas.SetActive(true);
    16.     }
    There is no other reference to containerCuestionary before or after, or anywhere else anywhere, and after the first time this whole thing runs... it works as expected. No idea whats going on here.

    Has this happened to anyone else before? is it a bug or there is something else going on? Did missed something?
     
  2. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Add the gameobject to the log statement.

    Code (csharp):
    1.  
    2. Debug.Log(container.activeSelf, container);
    3.  
    click on the statement in the console and it will highlight the gameobject. This double checks that you're looking at the right thing :)

    Otherwise - probably not a bug in Unity; unless you can simplify the use case.
     
  3. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    Thanks for that, i tried it and it was as expected, I already triple checked, quadrupled with this one, everything, the referenced object is correct, the thing is that this only works right the second time its executed.

    I have 3 classes involved, I've checked all 3, I checked other classes in the scene. The only reference to the object is containerCuestionary in the whole scene. This is just broken. If I manually set it to active, everything else on that execution sequence worked as intended except turning that GameObject active.
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    When you say not in the inspector do you mean the check box isn't checked?
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Make sure you don't have something else turning it off as well. And I don't mean with the variable containerCuestionary. You may have another variable somewhere targeting the same object. Just a thought as it being broken isn't as likely (possible, just not as likely with this).
     
  6. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    The checkbox isn't checked, and the neither itself or its children are showing (Its an UI). So it's not active, but it believes it is.

    As I said already checked, the only reference anywhere is in containerCuestionary, it's parent is active as well.
     
  7. whileBreak

    whileBreak

    Joined:
    Aug 28, 2014
    Posts:
    289
    Solved what was going on. The object started Deactivated, the first function to activate it was the one I was calling, so the next thing that it would do was to call Start(), where It would deactivate it (just to be sure that it always started deactivated).

    Changed the deactivation from Start() to Awake() and now it works as intended. Thanks everyone for the responses, sorry for this mess.
     
  8. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    I find putting a Debug.Log inside OnEnable/OnDisable a useful tool for debugging issues like this, just follow the callstack to find out where activation/deactivation starts.
     
    whileBreak likes this.