Another infamous NullReferenceException D:

I’m in a project for my internship and they asked me to change some assets. (basically, switch some prefabs for another prefab).

So, these prefabs were tagged with a different tag, therefore I tagged one of the new prefabs that they gave me with the tag that the old prefabs had (the one that I had to put for the old one). Next, to that, I started to add the components that the old prefabs have to the new prefab. I added the scripts that they had been attached to the older ones (So basically at this point you can be sure that I was creating some clones of over older prefabs.) The thing is, when I plug the prefabs to the GameManager. I get the NullReferenceException error. Now I will copy/paste the console error:

Console Error:

NullReferenceException: Object reference not set to an instance of an object
_Script.Manager.Level12.JellyfishManager.CreateInitialJellyfish (Level12.Words word, System.Single withdealay) (at Assets/_Script/Manager/Level12/JellyfishManager.cs:99)
_Script.Manager.Level12.GameManager.CreateInitialJellyfish () (at Assets/_Script/Manager/Level12/GameManager.cs:128)
_Script.Manager.Level12.MainMenuManager.<Awake>m__0 () (at Assets/_Script/Manager/Level12/MainMenuManager.cs:70)
UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:154)
UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:637)
UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:773)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:52)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update()

When I click the error to see where the error should be, it prompted to the JellyFishManager.cs, more accurately this function: (component.SetColor():wink: is where the error is pointing.

        public void CreateInitialJellyfish(Words word, float withdealay = 0f)
        {
            //jellyfish Object .
            GameObject jellyfish = null;
            //create jellyfish .
            jellyfish = Instantiate(JellyfishArray[(int) JellyFishType.KingJellyFish]);
            //Rename the object
            jellyfish.name = "BigInitialJellyfish";
            //set tag
            jellyfish.tag = "JellyFish";
            //get component .
            var component = jellyfish.GetComponent<InitialJellyFish>();
            //set color.
            component.SetColor();
            //set word.
            component.SetWord(word);
            //if wuth delay parameter is not overwritten.
            //component.AnimateJellyfish(!(withdealay <= 0f) ? withdealay : InitialJellyfishkAnimationDelay);
        }

The function derives from the InitialJellyFish.cs script, more accurately, this function:

        public void SetColor()
        {
            //Jellyfish color .
            var jellyfishColor = JellyfishColors[Random.Range(0, JellyfishColors.Count)];
            //Text color .
            var jellyfishTextColor = JellyfishTextColor[Random.Range(0, JellyfishTextColor.Count)];
            //set the color of text and Jellyfish .
            SetColor(jellyfishColor, jellyfishTextColor);
        }

And that is where I lost the trace of the error, and my thoughts, lol. If somebody is so kind of helping me with this. If you guys want more details about the project I can MP you so I can work this out and move on with the other problems.

You say the error occurs on component.SetColor();. The only object reference on that line is component, so you know that component is null. And that comes from the immediately preceding line, var component = jellyfish.GetComponent<InitialJellyFish>();. The only way that returns null is if your jellyfish doesn’t have an InitialJellyFish component.

I believe you said you were copying components over one by one; my guess is you haven’t yet copied over this InitialJellyFish component, which the code requires.

You are a master of codes! Well, now I get the ArgumentOutOfRangeException.
~~~csharp
ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
System.ThrowHelper.ThrowArgumentOutOfRangeException (System.ExceptionArgument argument, System.ExceptionResource resource) (at /Users/builduser/buildslave/mono/build/mcs/class/referencesource/mscorlib/system/throwhelper.cs:93)
System.ThrowHelper.ThrowArgumentOutOfRangeException () (at /Users/builduser/buildslave/mono/build/mcs/class/referencesource/mscorlib/system/throwhelper.cs:56)
System.Collections.Generic.List1[T].get_Item (System.Int32 index) (at /Users/builduser/buildslave/mono/build/mcs/class/referencesource/mscorlib/system/collections/generic/list.cs:181) _Script.Component.Level12.InitialJellyFish.SetColor () (at Assets/_Script/Component/Level12/InitialJellyFish.cs:140) _Script.Manager.Level12.JellyfishManager.CreateInitialJellyfish (Level12.Words word, System.Single withdealay) (at Assets/_Script/Manager/Level12/JellyfishManager.cs:99) _Script.Manager.Level12.GameManager.CreateInitialJellyfish () (at Assets/_Script/Manager/Level12/GameManager.cs:128) _Script.Manager.Level12.MainMenuManager.<Awake>m__0 () (at Assets/_Script/Manager/Level12/MainMenuManager.cs:70) UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:154) UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:637) UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:773) UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:52) UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45) UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50) UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction1[T1] functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update()

~~~
What I did, was adding the InitialiJellyfish Compoent. With this. the NullReferenceException is gone but now the other error occurs. I think it comes from an array but when I cick it no code is opened.

There’s a couple key lines in this error:

This line indicates the error is associated with a List.

System.Collections.Generic.List`1[T].get_Item (System.Int32 index) (at /Users/builduser/buildslave/mono/build/mcs/class/referencesource/mscorlib/system/collections/generic/list.cs:181)

And the line directly under it indicates it’s in your InitialJellyFish script on line 140, inside of the SetColor method:

_Script.Component.Level12.InitialJellyFish.SetColor () (at Assets/_Script/Component/Level12/InitialJellyFish.cs:140)

You are referencing two Lists in your SetColor method: JellyfishColors and JellyfishTextColor.
Check these lists to see if they have any values set.

Thank you for the clarification.

This is the script that I modified:

        public void SetColor()
        {
            //Jellyfish color .
            var jellyfishColor = JellyfishColors[Random.Range(0, JellyfishColors.Count -1)];
            //Text color .
            var jellyfishTextColor = JellyfishTextColor[Random.Range(0, JellyfishTextColor.Count -1)];
            //set the color of text and Jellyfish .
            SetColor(jellyfishColor, jellyfishTextColor);
        }

but the problem persists

By the way:

        private void SetColor(Color withJellyfishColor, Color withWordTextColor)
        {
            /*//select shader or shader
            var shader = Shader;
            //create materials with flag.
            var mat = new Material(shader) {hideFlags = HideFlags.HideAndDontSave};
            //set color.
            mat.SetColor("_EmissionColor", withJellyfishColor);
            //get the attached renderer .
            var rend = JellyfishObject.GetComponentInChildren<Renderer>();*/

            JellyfishObject.GetComponentInChildren<Renderer>().material.SetColor("_EmissionColor",withJellyfishColor);

           
            //set the materila.
            //rend.material = mat;
            //set color of word text .
            WordText.color = withWordTextColor;
        }

^ this is the original function. The public one was being overloaded.

I’m having troubles to debug this function. Can anyone help me do it?

I think @Vryken already told you what to do. Subtracting 1 from the count doesn’t help; if your lists are empty, any index is out of range.

Has an internship, can’t solve null ref exceptions.

This is why I can’t stand most every game developer in the greater Seattle area. Today I got blamed for a known issue related to me taking the initiative to use stuff in preview. The reply “use version control” is being used as a pedantic personal attack when I have legitimate (in this case non-) issues.

It’s hard not to resent reading something like that. I watched the kids in that liberal arts program watch cartoons in class. It’s entirely possible you deserve to fail. These are issues a professional developer should be able to approach alone.

ArgumentOutOfRangeException.

You say this like it means nothing to you even though it’s literally elementary to computer programming. Do your own homework, you aren’t in school any more.

You’re on internship, it’s your host company’s job to show you how to get around, isn’t it?

Whoa! Cowboy. I’m debugging codes that I didn’t make and there is not much documentation on it either. So if you haven’t come here to say something to help, then you could save it. Plus I fixed it already. So I’m not destined to fail xD.

And I’m pretty sure that you consulted or asked for help in a piece of code that you haven’t written. So don’t come here and acting all high and mighty.

Now, if a mod would be so kind to close this thread that would be nice.