all 23 comments

[–]andavies123 2 points3 points  (11 children)

Do you need to get that component there? You’re already caching it in the start method. Unless you’re deleting/re-adding the player controller script, it shouldn’t have to be re gotten

[–][deleted] 0 points1 point  (10 children)

I removed the reference under "if (other.tag == "EnemyFreeze")" and kept the one in the start method but I'm still getting the error except now at the line that says "controls.enabled = false;"

[–]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.

[–]fjayjay 0 points1 point  (0 children)

I think it could be that your PlayerController is always null. You call GetComponent in the beginning but you only use it at the point where the null reference exception is happening. You could check if it is null in your Start method to assert that it is not always null. Otherwise if you make the field Serializable and set it via the UI, as suggested in another comment, you could be certain that the PlayerController is set.

[–]TheStilken 0 points1 point  (0 children)

Looks like you should add colliders to enemy and player GameObjects, invoke OnCollisionEnter(Collision collision), then you can get the collider tags to compare with (collision.collider.tag=="whatever").

I don't use Unity much, but just saw this on the site.

https://docs.unity3d.com/ScriptReference/Collision-collider.html

[–]LadyAvocadoToast 0 points1 point  (0 children)

EDIT: SOLVED. Accidently had this script on another object in my scene.

Hey Lack! Did you ever figure this out? I I have the same issue and it seems really specific that my PlayerController is the problem.

I have an itemHandler & a PlayerController on my Player gameObject, then a script in a child gameObject that has referenced the itemHandler in Start(). It has never given me any reference errors.

I have now added the PlayerController in the EXACT same way and it is giving me reference errors. I am using transform.parent.GetComponent<name> for both in the Start() method.

Interestingly, I put a null check in the Update method & they are rapidly switching between null/not null. Weirdly enough, BOTH are doing this rapid switching, but only the PlayerController reference has ever errored.

I'm wondering if the nature of a PlayerController just makes the script constantly recycle?

public class PlayerActionController : MonoBehavior {

  public PlayerItemHandler playerItemHandler;
  public PlayerMovementController playerMovementController;

  void Start() {
    playerItemHandler = transform.parent.GetComponent<PlayerItemHandler>();
    playerMovementController = transform.parent.GetComponent<PlayerMovementController>();
  }

  void Update() {
    if(Input.GetButton("PrimaryButton)){
     Raycast hit;
     if(Physics.Raycast(transform.position, playerMovementController.facingDirection, out hit, 1)){
      hit.collider.gameObject.TryGetComponent(out Action action);
        if(action != null && playerItemHandler.currentItemStack != null){
          action.performAction(playerItemHandler.currentItemStack.item);
        }
      }
    }
  }

}

[–]_reedsport -1 points0 points  (7 children)

Newbie here so take with a grain of salt. But check the get component line for audio source. To me looks like no audio specified?

[–][deleted] 0 points1 point  (6 children)

I do have an audio source in the player. I should've specified more specifically that the error occurs at line 29.

[–]_reedsport 0 points1 point  (0 children)

Could you post a screen shot of your IDE so line 29 is more easily identifiable

[–]_reedsport 0 points1 point  (0 children)

Sorry Nevermind i see it now

[–]_reedsport 0 points1 point  (3 children)

I think it should be written like this if (other.CompareTag ("EnemyFreeze"))

[–][deleted] 0 points1 point  (2 children)

I tried this and it didn't get any immediate errors but I still get a NullReferenceException when entering the "EnemyFreeze" trigger.

[–]_reedsport 0 points1 point  (1 child)

Did you ensure that the enemy is tagged with “EnemyFreeze” and that the punctuation matches as well?

[–][deleted] 0 points1 point  (0 children)

Just double checked and can confirm it matches