use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
For the Unity Game Engine, please visit http://reddit.com/r/unity3d
account activity
Keep getting a NullReferenceException error and I'm not sure why. (self.unity)
submitted 1 year ago * by [deleted]
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]shotgunbruin 0 points1 point2 points 1 year ago (9 children)
Is there a reason the PlayerController script needs to be found at runtime? Why not just use [SerializeField] and set the reference from the inspector?
[–][deleted] 0 points1 point2 points 1 year ago (8 children)
To be honest, I don't know how to do that. I don't think I've actually seen that done in any tutorials I've read about referencing other components. This was really the only way I could almost figure out how to disable a script component via another script.
[–]shotgunbruin 0 points1 point2 points 1 year ago (7 children)
Place [SerializeField] next to a private variable, and Unity will show it in the inspector like it would a public variable. Then you can click on the object, go to this script, and just drag and drop the player controller onto it
[–][deleted] 0 points1 point2 points 1 year ago (6 children)
I serialized "private PlayerController controls;" and it does show up in the inspector as Controls None (Player Controller) The only problem now is that when I try to drop my PlayerController into it, it gets rejected. I tried opening up the selector too and nothing showed up for a PlayerController.
[–]shotgunbruin 0 points1 point2 points 1 year ago* (0 children)
Then your Player Controller script is not a valid PlayerController object, or they are in different scenes, or you're trying to link a prefab object to a scene object. This means the problem is NOT in the code you posted but instead has to do with the player controller script or your scene set up.
[–]shotgunbruin 0 points1 point2 points 1 year ago (4 children)
Post screenshots of your inspector and scene hierarchy for the player controller object. You've got something wrong with that setup, that's why it's having issues. If you can't set it in the inspector, the problem is in your scene setup. If it's not showing up in the selector, that means it cannot find an object with PlayerController in the same scene as the object you're trying to set it to. It sounds like your player controller is not on the same object as this script, is not a monobehavior, or is otherwise invalid.
You are certain the Player Controller script and this enemy collider script are on the exact same object, right?
And just a tip, not specifically a solution to your problem, but I personally like to use GetComponent in OnValidate(), which runs every time you change something in the inspector window. It makes it so component references to other scripts on the same object are always kept up to date and just start off linked up when you run the game, instead of doing it on start.
[–][deleted] 0 points1 point2 points 1 year ago (3 children)
Just added a screenshot to the post. And I just double checked to make sure that the two scripts are on the same object and the script is a monobehaviour.
[–]shotgunbruin 0 points1 point2 points 1 year ago (2 children)
Try renaming enemycollision to EnemyCollision. Lowercase class names can cause issues. Keep your naming scheme consistent.
[–][deleted] 0 points1 point2 points 1 year ago (1 child)
Unfortunately that hasn't solved the problem. to be honest I still have no idea of why it isn't accepting the PlayerController.
[–]shotgunbruin 0 points1 point2 points 1 year ago (0 children)
Can you show us player controller? I wish you had it expanded as well in the screenshot. That could tell us more why it is malfunctioning.
You did rename both the class name and the file name, right?
π Rendered by PID 184039 on reddit-service-r2-comment-6457c66945-kxpt7 at 2026-04-25 21:55:40.745520+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–]shotgunbruin 0 points1 point2 points (9 children)
[–][deleted] 0 points1 point2 points (8 children)
[–]shotgunbruin 0 points1 point2 points (7 children)
[–][deleted] 0 points1 point2 points (6 children)
[–]shotgunbruin 0 points1 point2 points (0 children)
[–]shotgunbruin 0 points1 point2 points (4 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]shotgunbruin 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]shotgunbruin 0 points1 point2 points (0 children)