#Null Reference Exception Error

1 messages · Page 1 of 1 (latest)

queen citrus
#

Hey, guys! Can you help me please with Null Reference that I am dealing with? So, I am making a flappy bird game and whenever my player enters the collider of the other object(gem) it disappears and my particle is being instantiated with object pooling method. So, what's happening basically is that instead of returning my gem particle game object back to its pool for one reason I am destroying it or something but as I see on my code I don't have any Destroy() method or something that destroys the object I have replaced everything. Check what is happening in the video below.

#

ObjectPooling.cs

#
using UnityEngine;

public class ObjectPooling : MonoBehaviour
{
    public static ObjectPooling Instance;

    [Header("POOLING FIELDS")]
    public GameObject objectPrefab;
    [SerializeField] private int poolSize = 50;
    [SerializeField] private bool autoGrow = false;
    [SerializeField] private List<GameObject> objectPoolingList = new List<GameObject>();

    private void Awake()
    {
        for (int i = 0; i < poolSize; i++)
        {
            CreateObject();
        }
    }

    private GameObject CreateObject()
    {
        GameObject obj = Instantiate(objectPrefab, Vector3.zero, Quaternion.identity);
        objectPoolingList.Add(obj);
        obj.SetActive(false);
        return obj;
    }

    public GameObject GetPooledObject()
    {
        GameObject pooledObject = null;
        foreach (GameObject obj in objectPoolingList)
        {
            if (obj != null && !obj.activeInHierarchy)
            {
                pooledObject = obj;
                break;
            }
        }
        if (pooledObject == null)
        {
            if (autoGrow)
            {     
                pooledObject = CreateObject();
            }
            else
            {
                Debug.LogWarning("Unable to create object but it is required");
            }
        }
        return pooledObject;
    }
}```
light breach
#

are these particles?

queen citrus
#

Yeah join the thread I will

light breach
#

they probably are set to Destroy

queen citrus
#

Tell digit to join the thread we are going to discuss it here

#

Check it out

#

It is a particle that I am spawning

light breach
queen citrus
#

How?

#

In my code I dont have anything that destroys it

#

I am pretty sure of that

light breach
#

Is that it Stop Action

#

i forgot

queen citrus
#

except if I lose something like a script

queen citrus
#

Wdym?

dreamy hollow
#

Put a script on that object that logs something in OnDestroy, then check the stack trace to see what calls destroy

light breach
queen citrus
#

So, you think that's why it's happening?

light breach
#

no, it would've been Destroy but its not.
Did you try what digi said, put log inside the object

queen citrus
#

Let me check for a min documentation about that

queen citrus
#

Or debug.log

#

If you meant the tree log I just did it the problem is with my gem particle

#

My tree logs are fine and collision is fine

light breach
#

I never mentioned anything about trees

queen citrus
#

No sorry

#

Collision and gem particles are not fine

#

they are not returned

#

only the logs

light breach
#

on the particle object you put a script ?

#

yu only need 1 method

queen citrus
#

How to call the script

#

Like a new class to make

light breach
#

Test

queen citrus
#

Ok

#

And put it inside my particle prefab?

light breach
#
void OnDestroy()
{
  Debug.Log(name + " Was Destroyed");
}
#

then check where the call comes from

light breach
#

the prefab yes

queen citrus
#

Yeah I done that you said

#

It says that the gem particle is destroyed

light breach
#

it print the log, select the log and screenshot

#

it shows the full strack trace

queen citrus
#

Here what it says for my collision particle as well

#

Here is the gem particle log

light breach
#

hmm thats strange..

queen citrus
#

So, how can I continue tracing the issue?

#

There is no information

#

Maybe thats the issue because I am getting that warning

#

It says that the referenced script is missing

#

I am not sure what it means

#

what's missing actually?

light breach
#

maybe you saved something / script during playmode?

queen citrus
#

Inspect my scripts whenever you have time guys I am going to try and find whats happening. Don't know yet how to trace the issue. I am going to show you the line that error occurs the null reference.

light breach
queen citrus
#

There is a command?

light breach
#

ctrl + F

queen citrus
#

What I am going to write here?

#

It says was not found Destroy

light breach
#

first you have to set it to Current project /

#

then you need FIND ALL

queen citrus
#

Yeah I have only OnDestroy method that I wrote before

light breach
#

did you write Destroy in search or OnDestroy

queen citrus
#

I wrote Destroy

#

Also, I just find out where is the source of the issue

#

So, actually it is in ObjectPooling.cs

#

Unable to create object but it is required UnityEngine.Debug:LogWarning (object) ObjectPooling:GetPooledObject () (at Assets/Scripts/GameScene/ObjectPooling.cs:50) PlayerController:OnTriggerEnter2D (UnityEngine.Collider2D) (at Assets/Scripts/GameScene/PlayerController.cs:69)

#

Thats what it says

#

The issue persists first of all in ObjectPooling.cs and after that at the PlayerController.cs where I am calling it.

#
using UnityEngine;

public class ObjectPooling : MonoBehaviour
{
    public static ObjectPooling Instance;

    [Header("POOLING FIELDS")]
    public GameObject objectPrefab;
    [SerializeField] private int poolSize = 50;
    [SerializeField] private bool autoGrow = false;
    [SerializeField] private List<GameObject> objectPoolingList = new List<GameObject>();

    private void Awake()
    {
        Instance = this;
        for (int i = 0; i < poolSize; i++)
        {
            CreateObject();
        }
    }

    private GameObject CreateObject()
    {
        GameObject obj = Instantiate(objectPrefab, Vector3.zero, Quaternion.identity);
        objectPoolingList.Add(obj);
        obj.SetActive(false);
        return obj;
    }

    public GameObject GetPooledObject()
    {
        GameObject pooledObject = null;
        foreach (GameObject obj in objectPoolingList)
        {
            if (obj != null && !obj.activeInHierarchy)
            {
                pooledObject = obj;
                break;
            }
        }
 
        if (pooledObject == null)
        {
            if (autoGrow)
            {     
                pooledObject = CreateObject();
            }
            else
            {
                Debug.LogWarning("Unable to create object but it is required");
            }
        }
        return pooledObject;
    }
}```
#

Check the GetPooledObject()

#

Something happening there with the if statements

#

The pooled object is null because we give it this value so it is true and then it enters the inside if statement which is false. It never goes true

queen citrus
#

@light breach Hey! Are you available to help me?

light breach
queen citrus
#

Nothing when you get back we will discuss it whenever you will have time.

#

Don't worry do you job 🙂

#

I am not in hurry

queen citrus
#

@light breach Hello! Are you available for 20 minutes to help me with my issue?

light breach
#

sure but only this time, post on code channel next time,.I'd rather not get @ with questions.

queen citrus
#

Ok! So, where to start? What do you want to show you? I have my object pooling.cs and my player controller.cs. I found out where is the source of the issue. It is in ObjectPooling.cs and PlayerController.cs at the specific lines I am going to tell you.

light breach
queen citrus
light breach
#

do you have any scripts on gems ?

light breach
queen citrus
#

I ve made that watch some tutorials got some help from chat gpt and tried to make that.

#

That was the first time I was making that I am not sure if I implement it correctly but I am not instantiating things as I did before

#

I am using somehow if not completely object pooling

#

For instantiating

light breach
#

using GPT is not very good cause it makes nonsense code

queen citrus
#

activating and deactivate things

#

That's trie

#

true

#

Sometimes it makes non sense

light breach
#

I would lookup how the Unity one is

queen citrus
#

But it helps for simple issues

#

Sometimes

light breach
#

issue is probably you're getting a poolobject to Active before returning the object first

queen citrus
#

What is happening is that

#

inside my gem objecte pooling list when my player collects the gems I have pool size 10 it says missing game object till the final game object and then it starts to create again the game object and immediately set it back to missing game object

#

so it is something like

#

1-10 missing

#

I have

#

1-10 gem game objects and when I am collecting them

#

it says

light breach
#

I would just remake the pooling

queen citrus
#

missing for each till the 10

light breach
#

and make it properly, actually learn how to use it

queen citrus
#

I don't know yet how to implement it properly

light breach
#

then you should not be using it

queen citrus
#

I am not sure most of the code was from chat to tell you the truth

#

I wanted to make it because a guy told me to do that

light breach
#

regardless, you should not have a system in the game unless you know how it works

queen citrus
#

I was just instantiating and destroying game objects

#

rather thatn using object pool

light breach
#

its good for performance yes, but now you trying to implement something you don't understand and it broke

queen citrus
#

the guy told me to make that

#

Do I have to remove it now

#

I have two options

light breach
#

You should leave it how you had it when it was working.. Then learn sepearely the proper way to pool untill you understand it

#

when you do that, you add the optimization

queen citrus
#

Either I am removing it and making it like I had it before or you just help me remake that object pooling

#

I think I dont have other alternative

light breach
#

I told you how to do it, put it like before since you understood what it was doing

#

then with time, even in a separate project..Play around with object pooling

queen citrus
#

The guy told me what it does

light breach
#

its dead simple now with the unity package they added

queen citrus
#

I understood somehow

#

What it does

light breach
#

not what it does you dont understsand how its working cause you wrote it overcomplicated than one unity provides

queen citrus
#

Rather than instantiating and destroyign game objects it activates object from the pool whenever I want it to activate and deactivate it set these objects as inactive in hierarchy

light breach
#

that theory behind is ok to know but is not enough

#

You have to understand HOW it does what it does

queen citrus
#

I want to understand the whole syntax of this method

#

step by step

light breach
#

I sent you 2 docs already

queen citrus
#

Are there different variations?

light breach
#

there are videos too, make sure they are the UNITY Pooling they added , not the custom ones

queen citrus
#

Or only one that all of us implement when making object pool

light breach
#

the one from UnityEngine.Pool

#

not a custom one

#

people make their own version, no need now. Unity has package

#

dead simple

#

its 3 methods

queen citrus
#

It is in docs you send me?

#

The one of Unity?

#

with 3 methods?

light breach
#

well 4 methods but still..

queen citrus
#

I will try to read these docs you send watch some tutorials and if you can send me some tutorials from people that use the unity original method of object pooling. I have to learn the methodology of how to implement it on my games.

light breach
# queen citrus I will try to read these docs you send watch some tutorials and if you can send ...

In this video, you’ll learn more about object pooling, how it can provide performance optimization by reducing the processing power required of the CPU to run repetitive create and destroy calls, and how to use it in your Unity projects.

⭐ Learn more about programming design patterns in this e-book: https://on.unity.com/3O6tR8J
⭐ Check out Unit...

▶ Play video
queen citrus
#

I have another question right now not related with that. I just enter Unity Learn with my account and I see zero progress. For one reason, I lost all of my progress do you know why this is happening because last time I remember I had so much exp and all of that?

#

I had a lot of tutorials saved there that I wanted to watch and courses and all of that

light breach
#

I'm not sure , I haven't used the new website much

queen citrus
#

I have heard that have happened to a lot of people

#

in reddit

light breach
#

u sure its the correct account /email ?

queen citrus
#

Yeah I had two accounts the one now I am in is my main

#

A guy says in reddit that there is a way to reset your account

#

But I am not sure how

#

Maybe I can contact unity

#

About that issue