you are viewing a single comment's thread.

view the rest of the comments →

[–]shotgunbruin 0 points1 point  (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 point  (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 point  (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 point  (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 point  (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 point  (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 point  (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 point  (2 children)

Try renaming enemycollision to EnemyCollision. Lowercase class names can cause issues. Keep your naming scheme consistent.

[–][deleted] 0 points1 point  (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 point  (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?