Search Unity

Fixed Joint Issue with OnTriggerStay()

Discussion in 'Scripting' started by The-NightflyAZ, Apr 27, 2015.

  1. The-NightflyAZ

    The-NightflyAZ

    Joined:
    Apr 27, 2015
    Posts:
    17
    Hi all! I'm hoping someone can quickly see the error in my scripting because this is driving me nuts.

    I have a vehicle with a tractor beam to pick up and drop rigid body game objects. I'm using the fixed joint component to do the parenting of the rigid bodies. I can turn on my tractor beam and pick things up but when I toggle off the tractor beam off, the lines of script that would destroy the fixed joint and let the attached object go don't ever seem to execute. I can see the variable to toggle the tractor beam on and off is working but the
    else if(tractorBeamToggle.tractorBeamOn == false) doesn't get the news.

    Thanks in advance for any insights.

    Here's the entire script.

    using UnityEngine;
    using System.Collections;

    public class FixedJointKepressTwo : MonoBehaviour
    {
    //variables to check variable in player script
    public GameObject player;
    private ship_Movement tractorBeamToggle; //there is a bool that is in ship movement script to indicate whether the tractor beam is on/off.

    public Rigidbody tractorBeam;
    public FixedJoint fixedJoint;


    void Start ()
    {

    fixedJoint = gameObject.AddComponent<FixedJoint> ();//Creating the fixed joint component
    tractorBeamToggle = player.GetComponent<ship_Movement>(); //Create the reference to the ship_Movement script which has the tractor beam toggle variable

    }

    void FixedUpdate()
    {
    if (fixedJoint == null)
    {
    CreateNewFixedJoint(); //to recreate the fixed joint that is destroyed theoretically when I turn the tractor beam off.

    }

    }

    void OnTriggerStay(Collider other)
    {

    if (other.collider.tag == "TractorBeam") //only want object to connect to the tractor beam object
    {
    Debug.Log ("Touching the tractor beam");

    if(tractorBeamToggle.tractorBeamOn == true)
    {
    fixedJoint.connectedBody = tractorBeam; //connect fixed joint to object stored in tractorBeam.
    }

    else if(tractorBeamToggle.tractorBeamOn == false)//this is the problem here. This never seems to execute if I've already connected the cargo to the tractor beam. I can't release the cargo ever.
    {
    Debug.Log ("Not going to attach anything here");
    Destroy (fixedJoint);

    }

    }
    }


    void CreateNewFixedJoint()
    {
    fixedJoint = gameObject.AddComponent<FixedJoint> ();//Creating a new fixed joint component
    }
    }