- Home /
Question by
Blast73 · Feb 17, 2016 at 05:07 PM ·
c#scripting problemrigidbody2d2d-physics
Rigidbody2D.IsKinematic giving object reference not set?
In the game the player picks up parts from a hook and drops them. I can pick them up from the hook but when they're supposed to be dropped nothing happens and kinematic isn't turned off for some reason.
using UnityEngine;
using System.Collections;
public class rocketPartInteraction : MonoBehaviour {
public GameObject rocketpart;
private GameObject hand;
public bool holding;
private bool pickedUp;
private Rigidbody2D rocketrigid;
// Use this for initialization
void Start () {
holding = false;
pickedUp = false;
hand = GameObject.FindWithTag("Player");
Rigidbody2D rocketrigid = this.GetComponent<Rigidbody2D>();
rocketrigid.isKinematic = true;
}
// Update is called once per frame
void FixedUpdate () {
if (holding) {
this.rocketpart.transform.position = hand.transform.position + new Vector3(1, 3, 0);
}
if (pickedUp)
rocketrigid.isKinematic = false; <--- this is the problem
}
void OnTriggerStay2D(Collider2D other){
if (other.gameObject.tag == "Player") {
if (Input.GetKeyDown ("space") && !holding) {
//Debug.Log ("Pickup Works");
holding = true;
}
else if (Input.GetKeyDown ("space") && holding) {
holding = false;
pickedUp = true;
}
}
}
}
NullReferenceException: Object reference not set to an instance of an object rocketPartInteraction.FixedUpdate () (at Assets/Scripts/rocketPartInteraction.cs:29)
also if anyone has a better suggestion on giving the item gravity that would be appreciated as well.
Comment
Your answer
Follow this Question
Related Questions
How to make object bounce from one bound to another? 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers