NullReference. Need Help.

I am using a variable from another script (facingRight). I get the error NullReferenceException, and I cant figure out what exactly I need to reference.

public class BulletTrail : MonoBehaviour {

    public int moveSpeed = 0;

    private PlayerController playerController;

    void Awake()
    {
        playerController = transform.root.GetComponent<PlayerController>();
    }

    void Update()
    {
            if (!playerController.facingRight)
            {
                transform.Translate(Vector3.left * Time.deltaTime * moveSpeed);
                Destroy(gameObject, 1);
            }
            if (playerController.facingRight)
            {
                transform.Translate(Vector3.right * Time.deltaTime * moveSpeed);
                Destroy(gameObject, 1);
            }
    }
}

Yes I know that it cant be possible. public bool StartTemp; if(StartTemp) { public InputField iField; public GameObject Template; } But I need editor script to drag GameObject and InputField in inspector if and only if bool is true. If bool is false then dont show that two field in inspector. @Landern

–

1 Answer

1

I’m guessing playerController is failing to get the component, which throws the error when you try to use it.

 void Awake()
 {
     playerController = transform.root.GetComponent<PlayerController>();
 }

^ This, are you sure this is working? Verify with a Debug.Log:

Debug.Log("is playerController null? " + (playerController == null));

Make adjustments so playerController is not null.

I put that in and it says its true and then spams me with the error.

–

PlayerController component needs to be on a GameObject so it can be found

–