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.
#Null Reference Exception Error
1 messages · Page 1 of 1 (latest)
PlayerController.cs
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;
}
}```
Yeah join the thread I will
they probably are set to Destroy
Tell digit to join the thread we are going to discuss it here
Check it out
It is a particle that I am spawning
show that is not set to destroy
except if I lose something like a script
Put a script on that object that logs something in OnDestroy, then check the stack trace to see what calls destroy
Yeah nvm what I said then, stop action is set to none..
So, you think that's why it's happening?
no, it would've been Destroy but its not.
Did you try what digi said, put log inside the object
Let me check for a min documentation about that
You mean the tree log?
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
so put a script on Gem object
I never mentioned anything about trees
No sorry
Collision and gem particles are not fine
they are not returned
only the logs
Test
void OnDestroy()
{
Debug.Log(name + " Was Destroyed");
}
then check where the call comes from
it goes on GemParticleEffect object
the prefab yes
wa
it print the log, select the log and screenshot
it shows the full strack trace
Here what it says for my collision particle as well
Here is the gem particle log
hmm thats strange..
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?
maybe you saved something / script during playmode?
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.
do you know how to search all scripts?
There is a command?
PlayerController.cs
https://paste.mod.gg/adveuowdfmjr/0
A tool for sharing your source code with the world!
ObjectPooling.cs
https://paste.mod.gg/nmgdvxlpeoqf/1
A tool for sharing your source code with the world!
first of all doesnt match anything I shown
first you have to set it to Current project /
then you need FIND ALL
Yeah I have only OnDestroy method that I wrote before
SpawnManager.cs
https://paste.mod.gg/btetwjxqyhvh/2
A tool for sharing your source code with the world!
did you write Destroy in search or OnDestroy
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
@light breach Hey! Are you available to help me?
Im at work so not much but whats the problem ?
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
@light breach Hello! Are you available for 20 minutes to help me with my issue?
sure but only this time, post on code channel next time,.I'd rather not get @ with questions.
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.
send both scripts then
https://hatebin.com
ObjectPooling.cs
https://hatebin.com/qgosmdagpw
PlayerController.cs
https://hatebin.com/wfvpfhhozt
do you have any scripts on gems ?
where did you get this pooling from ?
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
using GPT is not very good cause it makes nonsense code
I would lookup how the Unity one is
issue is probably you're getting a poolobject to Active before returning the object first
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
I would just remake the pooling
missing for each till the 10
and make it properly, actually learn how to use it
I don't know yet how to implement it properly
then you should not be using it
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
regardless, you should not have a system in the game unless you know how it works
I was just instantiating and destroying game objects
rather thatn using object pool
its good for performance yes, but now you trying to implement something you don't understand and it broke
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
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
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
The guy told me what it does
its dead simple now with the unity package they added
not what it does you dont understsand how its working cause you wrote it overcomplicated than one unity provides
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
that theory behind is ok to know but is not enough
You have to understand HOW it does what it does
I sent you 2 docs already
Are there different variations?
there are videos too, make sure they are the UNITY Pooling they added , not the custom ones
Or only one that all of us implement when making object pool
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
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.
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...
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
I'm not sure , I haven't used the new website much
u sure its the correct account /email ?