Quantcast
Channel: Questions in topic: "parenting"
Viewing all articles
Browse latest Browse all 302

NullReferenceException with SwitchController script

$
0
0
My script wont seem to be working and I dont know why. It is supposed to tick whether your able to mount or not. If you are, and you press F, it should parent the player to the riderPivot and reset its position. Its also supposed to disable player movement and enable horse movement. But all I get when trying to do it is NullReferenceException: Object reference not set to an instance of an object. Any help is appreciated!!!! Heres my script using UnityEngine; using System.Collections; public class SwitchController : MonoBehaviour { public bool mounted; public bool mountable; public Transform riderPivot; private GameObject rider; void OnTriggerEnter(Collider rider) { if (rider.tag == ("Player")) { mountable = true; } else { mountable = false; } } void OnTriggerExit(Collider rider) { mountable = false; } void Update () { if (mountable == true && Input.GetKey (KeyCode.F)) { mounted = true; Mount (); } if (mounted == true && Input.GetKey (KeyCode.F)) { mounted = false; UnMount (); } } void Mount(){ rider.transform.position = riderPivot.position; rider.transform.SetParent (riderPivot); rider.GetComponent ().enabled = false; this.GetComponentInParent ().enabled = true; } void UnMount(){ rider.transform.position = this.transform.position; rider.transform.SetParent (null); rider.GetComponent ().enabled = true; this.GetComponentInParent ().enabled = false; } }

Viewing all articles
Browse latest Browse all 302

Trending Articles