I am encountering a series of errors when trying to attach fixed joints during runtime through a particular script. I have a little robot that moves by a grappling hook. The base of that hook (the arm) is attached to the robot through a couple joints. During gameplay, a button press instantiates and attaches another object - the grapnel head - to the arm and adds a velocity to the grapnel head. The same press will delete the grapnel. The head has a collider on it and this script:
using UnityEngine;
using System.Collections;
public class Grab : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void OnTriggerEnter(Collider other){
gameObject.GetComponent<Rigidbody> ().velocity = Vector3.zero;
FixedJoint sticky = gameObject.AddComponent<FixedJoint> ();
if (other.gameObject.GetComponent<Rigidbody> () != null) {
sticky.connectedBody = other.gameObject.GetComponent<Rigidbody>();
}
gameObject.GetComponent<Collider>().enabled = false;
}
}
This script is triggered when the grapnel encounters something, creates a fixed joint on the grapnel, and then does one of two things: nothing and allows the fixed joint to remain attached to the world, or attaches the grapnel to whatever rigidbody it encountered if the collider it hit had an associated rigidbody.
This works about 7/10 times. 1/10 times Unity straight crashes. 2/10 times Unity hangs and I get these errors:
All five of these objects are children of the whole player. One player contains three meshed objects and two empty ones used as anchors and joints. The only one that ever has its transform hard set is GrabbleHead_Red(Clone) when its position is set to the same point as the robot’s arm. It also seems that the head occasionally passes through a collider too far before realizing and stopping. This seems more likely to cause a crash or hang.
I’m inclined to believe that the script shown is the cause for the following reason: Everything functions fine without the script attached at all. Also, everything works fine when the script is attached and the body of the onTriggerEnter () function is commented out. The robot may fire the (albeit useless) grapple about without crashing. It’s only when any FixedJoints are added that the crashes or hangs occur.
This also seems to be a recent development. The script hadn’t changed at all over the past couple days, but only just today has there been any issue with the robot. Additionally, I removed all the changes that I have made since the problem started, and the problems persisted. If anyone/deity out there has any ideas on what hell this is, plz help.
Thanks!
Summary:
-Error believed to be from Fixed Joints
-Causing problems in transforms
-Error object transforms never set with script