NullReferenceException < strange behaviour using LISTS >

im not sure if the mix of using a LIST and a reference to an object inside an Coroutine cause an permament exception… look at this code im already calling the object One Code line before the exception with No problems so the object exist in the moment of the caller, i may think the list.add method is just used in the next frame… or something like that… in that case i would have problems … can anyone figure out reasons?

IEnumerator RowCheck(int Fila) 
    {

       
        for (int i = 0; i < 4; i++) 
        { // 0 , 1 , 2 , 3

            Vector3 posicion;   
            int buscador = Fila + i;
            string cadena1 = buscador.ToString();
            string color1 = "Null1";
            string color2 = "Null2";
            string color3 = "Null3";
            string color4 = "Null4";
            string color5 = "Null5";
            string color6 = "Null6";

            GameObject objeto1 = GameObject.Find(cadena1);

            if (objeto1)
            {
                if (objeto1.GetComponent<MainGameCube>().iamready == true)
                {
                    color1 = objeto1.GetComponent<MainGameCube>().elcolor;
                }
            }

            string cadena2 = (buscador + 1).ToString();
            string cadena3 = (buscador + 2).ToString();
            GameObject objeto2 = GameObject.Find(cadena2);
            GameObject objeto3 = GameObject.Find(cadena3);
            if (objeto2)
            {
                if (objeto2.GetComponent<MainGameCube>().iamready == true)
                {
                    color2 = objeto2.GetComponent<MainGameCube>().elcolor;
                }
            }
            if (objeto3)
            {
                if (objeto3.GetComponent<MainGameCube>().iamready == true)
                {
                    color3 = objeto3.GetComponent<MainGameCube>().elcolor;
                }
            }
            if (color1 == color2 && color2 == color3)
            {
                string collig = objeto1.name;
                objeto1.tag = "prematch";
                objeto2.tag = "prematch";
                objeto3.tag = "prematch";
                // ADD VERTICAL DESDENCER TO PREVIOUS CUBES
               
                destruidos.Add(collig);

etc.. etc.. etc..
yield something etc..

The exception jumps on the destruidos.Add(colling) telling me theres no refence of the object ( it means objeto1 reference has destroyed or not set at this moment and just a line before it sets to the same refeence a new tag…) im very confused… please help

What line is the exception occurring on exactly? And what is the exact error message?

You use GameObject.Find a lot which is not great.

Are you sure it’s the object and not the list itself that is not referenced? That would seem to be the case based on what I see here.

I guessed he meant line ‘54’ in his post. It would be super helpful if people included that information when they posted lol. I see that a lot.

1 Like

NullReferenceException: Object reference not set to an instance of an object
PD_Q_CAM+c__Iterator2.MoveNext () (at Assets/Scripts/QUEST/PD_Q_CAM.cs:200)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
c__Iterator1:MoveNext() (at Assets/Scripts/QUEST/PD_Q_CAM.cs:122)
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)

Yeah, just wanted to check because there is talk about the objeto1 thing also

If the line is line 54 (destruidos.Add(colling)) then your list is null. How do you setup your list?

1 Like

yes the error code is about line 54 of this post…

I set up the list
private List destruidos;
void Awake()
{
//;
destruidos = new List();
}

well, that looks proper. Is that the same script as where the error is?
Does the closing brace of the if statement surrounding the list.Add occur after you add it? (unseen in the script, so presume so, but just checking).

I think you might need to post the entire script. There’s so many subtle things that could be going wrong, it’ll take forever to go back and forth like this.

Lol uh huh. that, too…

i was considering swithing my GameObject.Find functions to GameObjectFindwithTag , but someone pointed a lot of bugs on this last one, so i probably put the GameObjects need inside Lists…assuming Lists work nicely ;(