- Home /
NullReferenceException: Object reference not set to an instance of an object
Hello Unity3D Community, first of all, I know there are like 100 Questions about this Error. I'm still not able to fix it.
The Error shows up in this line of code.
PlayerWeapons PlayerWeaponsComponent = GameObject.Find("FPS Player").GetComponent<PlayerWeapons>();
FPS Player is a Prefab attached to the Prefab Player, the Script PlayerWeapons is attached to FPS Player. The Prefab Player gets spawned / created later on Ingame (Clone)
Thanks for the Help (:
Your sincerely, Pwnographyz
Answer by DESTRUKTORR · Jun 10, 2014 at 10:39 PM
You would not get a null reference exception if you tried to do "GetComponent" for a component that was not found on a GameObject. It will simply return null. The null reference exception is coming from the fact that there (apparently) is no GameObject called "FPS Player". Check to ensure that you are using the exact name of the GameObject you are trying to find, and that it, in fact, exists when you make that call.
Try using the "error pause" option on the console, in the Unity Editor to ensure that the exact frame that the error occurs on is when you are checking to ensure that the GameObject is there.
Object exists, I wrote the Wrong name in my Post. It's correct with "FPS Weapons" in my Script.
Oh. I solved it.
PlayerWeapons PlayerWeaponsComponent = GameObject.Find("Player(Clone)").GetComponentInChildren<PlayerWeapons>();
Thanks for the help!
Your answer
Follow this Question
Related Questions
Pause Script Suddenly Stops Working 0 Answers
NullReferenceException help 1 Answer
Why am I getting this error ? 2 Answers
NullReferenceException: Object reference not set to an instance of an object 2 Answers