c# – Get values from ToggleGroup using labels in unity

Im trying to get values from a togglegroup by finding a label to recognize the answer in a survey made in unity.
But in the moment of getting the result, unity shows me a NullReferenceException cause it can’t found the object.

    {
        ArrPyR result = new  ArrPyR();

        pr = GrupoPreguntas.transform.Find("pregunta").gameObject;
        re = GrupoPreguntas.transform.Find("respuestas").gameObject;


         if (re.GetComponent<ToggleGroup>() != null)
            {
                for(int i = 0; i< re.transform.childCount; i++)
                {
                    if(re.transform.GetChild(i).GetComponent<Toggle>().isOn)
                    {
                        result.respuestas = re.transform.GetChild(i).Find("LabelSi").GetComponent<Text>().text;
                        break;
                    }
                }
                
            }else
            // {
            //      if(re.GetComponent<ToggleGroup>() !=null)
            //     {
            //      for (int i = 0; i< re.transform.childCount; i++)
            //         {
            //           if(re.transform.GetChild(i).GetComponent<Toggle>().isOn)
            //             {
            //                 result.respuestas = re.transform.GetChild(i).Find("LabelNo").GetComponent<Text>().text;
            //                  break;
                            
            //             }
            //         }
                    
            //      }
                 
            // }
            
            return result;

The application is getting first value named “labelSi” but the second value named “labelNo” is showing me the error so i commented it.

Source link