[Issue] Ojbect Missing My Script with ResourceManager & PoolManager

#Problem situation analysis

Implemented ResourceManager and PoolManager using addressable system.

My problem now is

  1. A Weapon object is created through DropManager.
  2. Weapon object is learned by the player. (Here, the object enters the Pool of PoolManager)
  3. It is automatically equipped to the player. (Here, the Weapon object in the PoolManager’s Pool is moved to the WeaponSlot in the play.)

The problem occurs in number 3.

Looking at the inspector window, you can see that Boomerang.cs (a specific weapon script) is not attached to the TestWeapon object in weaponSlot.

If a game object changes parents frequently, might it be impossible to find a specific script?

What makes it even more curious is
Before adding these ResouceManager and PoolManager, everything was running fine.

However, I would like to install these managers to optimize performance, but I have been seeing this error for 2 days.

Can I ask for help from someone who has experienced a similar problem or knows how to solve it?

1 Like

Yes, this may be a crucial, but depends on some factors.

  1. What the way you are looking for a script?
  2. Where are the starting and ending points of looking in terms of hierarchy? (provide screenshot of The Hierarchy window, video demo of parenting — any visualization)

Searching in runtime is a bad idea. Use caching for that scripts. My example:

1 Like

The answer is always the same… ALWAYS!

How to fix a NullReferenceException error

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

NullReference is the single most common error while programming. Fixing it is always the same.

Some notes on how to fix a NullReferenceException error in Unity3D:

http://plbm.com/?p=221

Based on the fact that you’re calling them Resources Manager and Object Pool, you probably have defective singleton implementations.

IF your scripts have DontDestroyOnLoad() and require you to drop them into the scene, then they are defective singletons. Don’t use defective singletons.

Some super-simple Singleton examples to take and modify:

Simple Unity3D Singleton (no predefined data):

Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:

These are pure-code solutions, DO NOT put anything into any scene, just access it via .Instance

Alternately you could start one up with a [RuntimeInitializeOnLoad] attribute.

There is never a reason to drag a GameObject into a scene if it will be DontDestroyOnLoad.

If you do:

  • you may drag it into something else that isn’t DDOL. FAIL
  • you may drag something else into it that isn’t DDOL. FAIL

Just DO NOT drag anything into a scene that will be marked DontDestroyOnLoad. Just Don’t Do It!

1 Like