hye guys i’ve been working on a game for mobile where as i use touch pad and buttons from the touch controls kit asset that i bought from assetstore, however there’s several error when running the game. Can someone help me? i really dont know how to fix the errors.
ERRORS:
1)NullReferenceException: Object reference not set to an instance of an object
TouchControlsKit.TCKInput.GetButtonDown (System.String buttonName) (at Assets/TouchControlsKit/Scripts/TCKInput.cs:703)
SkillWindow.Update () (at Assets/PlatformerKit/Script/SkillWindow.cs:45)
2)NullReferenceException: Object reference not set to an instance of an object
TouchControlsKit.TCKInput.GetAxis (System.String controllerName, AxisType axisType) (at Assets/TouchControlsKit/Scripts/TCKInput.cs:379)
PlayerInputController.Update () (at Assets/PlatformerKit/Script/PlayerInputController.cs:132)
NullReferenceException: Object reference not set to an instance of an object
TouchControlsKit.TCKInput.GetButton (System.String buttonName) (at Assets/TouchControlsKit/Scripts/TCKInput.cs:721)
AttackTrigger.Update () (at Assets/PlatformerKit/Script/AttackTrigger.cs:219)
Here’s my player input controller script :
using UnityEngine;
using System.Collections;
using TouchControlsKit;
[RequireComponent (typeof (CharacterMotor))]
public class PlayerInputController : MonoBehaviour {
private CharacterMotor motor;
private PlayerAnimation anim;
public float walkSpeed = 6.0f;
public float dashSpeed = 12.0f;
public float dashDuration = 0.5f;
private GameObject mainModel;
public bool groundDash = true;
public bool airDash = false;
public bool doubleJump = false;
public bool wallKick = false;
public GameObject dashEffect;
public GameObject wallKickEffect;
public GameObject wallSlideEffect;
[HideInInspector]
public bool dashing = false;
private bool jumpingDash = false;
private bool jumpingDown = false;
[HideInInspector]
public bool onWallSlide = false;
private bool airJump = false;
[HideInInspector]
public bool airMove = false;
[HideInInspector]
public bool onWallKick = false;
[HideInInspector]
public float originalX = 0.0f;
private float gravity = 20.0f;
//public Rigidbody rb;
//----------Sounds-------------
[System.Serializable]
public class MovementSound {
public AudioClip jumpVoice;
public AudioClip walkingSound;
public AudioClip dashVoice;
}
public MovementSound sound;
// Use this for initialization
void Start (){
motor = GetComponent<CharacterMotor>();
anim = GetComponent<PlayerAnimation>();
mainModel = GetComponent<Status>().mainModel;
gravity = motor.movement.gravity;
originalX = transform.position.x;
if(wallSlideEffect){
wallSlideEffect.SetActive(false);
}
if(dashEffect){
dashEffect.SetActive(false);
}
//rb = GetComponent<Rigidbody> ();
}
// Update is called once per frame
public void Update (){
Status stat = GetComponent<Status>();
if(transform.position.x != originalX){
transform.position = new Vector3(originalX , transform.position.y , transform.position.z);
}
if(stat.freeze || stat.flinch){
motor.inputMoveDirection = new Vector3(0,0,0);
return;
}
if(Time.timeScale == 0.0f){
return;
}
CharacterController controller = GetComponent<CharacterController>();
if(jumpingDash){
//Jump while dashing
if(TCKInput.GetButtonUp("jumpBtn")){
CancelDashJump();
}
//Vector3 jd = transform.TransformDirection(new Vector3(0 , 2 , Mathf.Abs(Input.GetAxis("Horizontal")) +1));
//controller.Move(jd * 5 * Time.deltaTime);
Vector3 jd = transform.TransformDirection(new Vector3(0 , 2 , Mathf.Abs(TCKInput.GetAxis( "dPad", AxisType.X )) +1));
controller.Move(jd * 5 * Time.deltaTime);
return;
}
if(jumpingDown){
//Vector3 jdown = transform.TransformDirection(new Vector3(0 , 0 , Mathf.Abs(Input.GetAxis("Horizontal")) +1));
//controller.Move(jdown * 4 * Time.deltaTime);
Vector3 jdown = transform.TransformDirection(new Vector3(0 , 0 , Mathf.Abs(TCKInput.GetAxis( "dPad", AxisType.X )) +1));
controller.Move(jdown * 4 * Time.deltaTime);
return;
}
if(onWallKick){
//Wall Kick
Vector3 wk = transform.TransformDirection(new Vector3(0 , 2.5f , -1));
controller.Move(wk * 6 * Time.deltaTime);
return;
}
if(airJump){
//Double Jump
//Vector3 aj = transform.TransformDirection(new Vector3(0 , 2.5f , Mathf.Abs(Input.GetAxis("Horizontal"))));
//controller.Move(aj * 4 * Time.deltaTime);
Vector3 aj = transform.TransformDirection(new Vector3(0 , 2.5f , Mathf.Abs(TCKInput.GetAxis( "dPad", AxisType.X ))));
controller.Move(aj * 4 * Time.deltaTime);
return;
}
if (dashing){
//Dash
if(TCKInput.GetButtonUp("dash") || Input.GetKeyUp(KeyCode.LeftShift) || !motor.canControl){
CancelDash();
}
if(TCKInput.GetButtonDown("jumpBtn") && (controller.collisionFlags & CollisionFlags.Below) != 0){
StartCoroutine("DashJump");
}
Vector3 fwd = transform.TransformDirection(Vector3.forward);
controller.Move(fwd * dashSpeed * Time.deltaTime);
//motor.inputMoveDirection = fwd;
return;
}
// Get the input vector from kayboard or analog stick
//Vector3 directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
//Vector3 directionVector = new Vector3(0 , 0, Input.GetAxis("Horizontal"));
Vector3 directionVector = new Vector3(0 , 0, TCKInput.GetAxis( "dPad", AxisType.X ));
//Vector3 directionVector = new Vector3(Input.GetAxis("Horizontal") , 0, 0);
if (directionVector != Vector3.zero) {
// Get the length of the directon vector and then normalize it
// Dividing by the length is cheaper than normalizing when we already have the length anyway
float directionLength = directionVector.magnitude;
directionVector = directionVector / directionLength;
// Make sure the length is no bigger than 1
directionLength = Mathf.Min(1, directionLength);
// Make the input vector more sensitive towards the extremes and less sensitive in the middle
// This makes it easier to control slow speeds when using analog sticks
directionLength = directionLength * directionLength;
// Multiply the normalized direction vector by the modified length
directionVector = directionVector * directionLength;
}
// Apply the direction to the CharacterMotor
//if(Input.GetAxis("Horizontal") > 0.1){
if(TCKInput.GetAxis( "dPad", AxisType.X ) > 0.1){
transform.eulerAngles = new Vector3 (transform.eulerAngles.x, 0, transform.eulerAngles.z);
}//else if(Input.GetAxis("Horizontal") < -0.1){
else if(TCKInput.GetAxis( "dPad", AxisType.X ) < -0.1){
transform.eulerAngles = new Vector3 (transform.eulerAngles.x, 180, transform.eulerAngles.z);
}
//motor.inputMoveDirection = transform.rotation * Vector3.forward;
//motor.inputMoveDirection = new Vector3(0 , 0, Input.GetAxis("Horizontal"));
motor.inputMoveDirection = new Vector3(0 , 0, TCKInput.GetAxis( "dPad", AxisType.X ));
//Double Jump Down
//if(TCKInput.GetButtonDown("jumpBtn") && !motor.grounded && doubleJump){
if(TCKInput.GetButtonDown("jumpBtn") && !motor.grounded ){
StartCoroutine("DoubleJumping");
}
if (controller.collisionFlags == CollisionFlags.None && onWallSlide){
CancelWallSlide();
}
if(TCKInput.GetButtonDown("jumpBtn") && sound.jumpVoice && motor.grounded){
GetComponent<AudioSource>().clip = sound.jumpVoice;
GetComponent<AudioSource>().Play();
}
//if(Input.GetAxis("Horizontal") != 0 && sound.walkingSound && !GetComponent<AudioSource>().isPlaying){
if(TCKInput.GetAxis( "dPad", AxisType.X ) != 0 && sound.walkingSound && !GetComponent<AudioSource>().isPlaying){
GetComponent<AudioSource>().clip = sound.walkingSound;
GetComponent<AudioSource>().Play();
}
//Activate Sprint
if(TCKInput.GetButtonUp("dash") && !dashing && groundDash || Input.GetKeyDown(KeyCode.LeftShift) && !dashing && groundDash){
//Dash
if((controller.collisionFlags & CollisionFlags.Below) != 0){
//StartCoroutine(Dash());
if(sound.dashVoice){
GetComponent<AudioSource>().clip = sound.dashVoice;
GetComponent<AudioSource>().Play();
}
StartCoroutine("Dash");
}else if(airDash && !onWallSlide){
if(sound.dashVoice){
GetComponent<AudioSource>().clip = sound.dashVoice;
GetComponent<AudioSource>().Play();
}
StartCoroutine("AirDash");
}
}
//motor.inputJump = Input.GetButton("Jump");
motor.inputJump = TCKInput.GetButton("jumpBtn");
//motor.inputJump = Input.GetKeyDown("c");
}
IEnumerator Dash(){
if(!dashing){
if(dashEffect){
dashEffect.SetActive(true);
}
dashing = true;
mainModel.GetComponent<Animation>().Play(anim.dash.name);
yield return new WaitForSeconds(dashDuration);
CancelDash();
}
}
void CancelDash(){
StopCoroutine("Dash");
if(dashEffect){
dashEffect.SetActive(false);
}
mainModel.GetComponent<Animation>().Stop(anim.dash.name);
motor.freezeGravity = false;
dashing = false;
}
IEnumerator AirDash(){
if(!dashing && !airMove){
if(dashEffect){
dashEffect.SetActive(true);
}
airMove = true;
dashing = true;
motor.freezeGravity = true;
mainModel.GetComponent<Animation>().Play(anim.dash.name);
yield return new WaitForSeconds(dashDuration);
motor.freezeGravity = false;
CancelDash();
}
}
public IEnumerator DoubleJumping(){
if(!airMove){
airMove = true;
airJump = true;
motor.freezeGravity = true;
yield return new WaitForSeconds(0.2f);
motor.freezeGravity = false;
airJump = false;
}
}
public IEnumerator DashJump(){
CancelDash();
motor.freezeGravity = true;
jumpingDash = true;
yield return new WaitForSeconds(0.25f);
CancelDashJump();
}
void CancelDashJump(){
if(jumpingDash){
jumpingDown = true;
}
jumpingDash = false;
motor.freezeGravity = false;
StopCoroutine("DashJump");
}
//void OnCollisionStay(Collision col) {
void OnControllerColliderHit(ControllerColliderHit col) {
CharacterController controller = GetComponent<CharacterController>();
if(jumpingDown){
jumpingDown = false;
}
if(airMove && wallKick && col.gameObject.tag != "Enemy" && motor.grounded){
//if(airMove && wallKick && col.gameObject.tag != "Enemy"){
airMove = false;
motor.freezeGravity = false;
}else if(airMove && wallKick && col.gameObject.tag != "Enemy" && controller.collisionFlags == CollisionFlags.Sides && col.gameObject.tag == "Wall"){
airMove = false;
motor.freezeGravity = false;
}else if(airMove && (controller.collisionFlags & CollisionFlags.Below) != 0){
airMove = false;
motor.freezeGravity = false;
}
if(col.gameObject.tag == "Wall"){
//(Input.GetButton("Horizontal") && !motor.grounded && controller.collisionFlags == CollisionFlags.Sides && Input.GetButton("Jump") && !motor.jumping.holdingJumpButton && wallKick){
if(TCKInput.GetAxis( "dPad", AxisType.X )>0||TCKInput.GetAxis( "dPad", AxisType.X )<0){
//if(!motor.grounded && controller.collisionFlags == CollisionFlags.Sides && Input.GetButton("Jump") && !motor.jumping.holdingJumpButton && wallKick)
if(!motor.grounded && controller.collisionFlags == CollisionFlags.Sides && TCKInput.GetButton("jumpBtn") && !motor.jumping.holdingJumpButton && wallKick)
StartCoroutine(WallJump());
}//else if(Input.GetButton("Horizontal") && !motor.grounded && controller.collisionFlags == CollisionFlags.Sides && motor.movement.velocity.z == 0 && motor.movement.velocity.y <= 0 && wallKick){
else if(TCKInput.GetAxis( "dPad", AxisType.X )>0||TCKInput.GetAxis( "dPad", AxisType.X )<0){
if(!motor.grounded && controller.collisionFlags == CollisionFlags.Sides && motor.movement.velocity.z == 0 && motor.movement.velocity.y <= 0 && wallKick)
//Wall Slide
CancelDashJump();
onWallSlide = true;
if(wallSlideEffect){
wallSlideEffect.SetActive(true);
}
mainModel.GetComponent<Animation>().Play(anim.wallSlide.name);
motor.movement.gravity = gravity / 4;
motor.movement.maxFallSpeed = 5;
}else if(onWallSlide){
CancelWallSlide();
}
}else if(onWallSlide){
CancelWallSlide();
}
}
void CancelWallSlide(){
if(wallSlideEffect){
wallSlideEffect.SetActive(false);
}
onWallSlide = false;
motor.movement.gravity = gravity;
motor.movement.maxFallSpeed = 20;
}
IEnumerator WallJump(){
CancelWallSlide();
CancelDashJump();
mainModel.GetComponent<Animation>().Play(anim.wallKick.name);
motor.freezeGravity = true;
onWallKick = true;
if(wallKickEffect){
Instantiate(wallKickEffect , transform.position , wallKickEffect.transform.rotation);
}
if(wallSlideEffect){
wallSlideEffect.SetActive(false);
}
yield return new WaitForSeconds(0.15f);
motor.freezeGravity = false;
onWallKick = false;
}
}