- Home /
Script working on some objects but not others
Hey,
So I have a headscratcher, I have a ragdoll with configurable joints, each with this script attached.
using UnityEngine;
using System.Collections;
public class ConfigurableJointProperties : MonoBehaviour
{
public float strength = 10000f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
ConfigurableJoint configurablejoint = gameObject.GetComponent<ConfigurableJoint>();
JointDrive angularYZDrive = new JointDrive();
angularYZDrive.mode = JointDriveMode.Position;
angularYZDrive.positionSpring = strength;
angularYZDrive.maximumForce = strength;
configurablejoint.angularYZDrive = angularYZDrive;
JointDrive angularXDrive = new JointDrive();
angularXDrive.mode = JointDriveMode.Position;
angularXDrive.positionSpring = strength;
angularXDrive.maximumForce = strength;
configurablejoint.angularXDrive = angularXDrive;
JointDrive xDrive = new JointDrive();
xDrive.mode = JointDriveMode.None;
xDrive.positionSpring = 0f;
configurablejoint.xDrive = xDrive;
JointDrive yDrive = new JointDrive();
yDrive.mode = JointDriveMode.None;
yDrive.positionSpring = 0f;
configurablejoint.yDrive = yDrive;
JointDrive zDrive = new JointDrive();
zDrive.mode = JointDriveMode.None;
zDrive.positionSpring = 0f;
configurablejoint.zDrive = zDrive;
}
}
This works fine and dandy, thing is its kind of a pain going through all the children and changing this when it needs changing so I created a master script which takes all these scripts and groups them together based on their location, seen here.
using UnityEngine;
using System.Collections;
public class RagdollMasterControlScript : MonoBehaviour
{
public GameObject root;
public GameObject head;
public GameObject neck;
public GameObject lowerSpine;
public GameObject upperSpine;
public GameObject pelvis;
public GameObject rightUpperArm;
public GameObject rightForearm;
public GameObject rightHand;
public GameObject leftUpperArm;
public GameObject leftForearm;
public GameObject leftHand;
public GameObject rightThigh;
public GameObject rightCalf;
public GameObject rightFoot;
public GameObject leftThigh;
public GameObject leftCalf;
public GameObject leftFoot;
public float rootPositionSpring = 10000f;
public float headPositionSpring = 10000f;
public float torsoPositionSpring = 10000f;
public float leftArmPositionSpring = 10000f;
public float rightArmPositionSpring = 10000f;
public float leftLegPositionSpring = 10000f;
public float rightLegPositionSpring = 10000f;
void Start ()
{
}
// Update is called once per frame
void FixedUpdate ()
{
head.GetComponent<ConfigurableJointProperties> ().strength = headPositionSpring;
neck.GetComponent<ConfigurableJointProperties> ().strength = headPositionSpring;
lowerSpine.GetComponent<ConfigurableJointProperties>().strength=torsoPositionSpring;
upperSpine.GetComponent<ConfigurableJointProperties>().strength=torsoPositionSpring;
pelvis.GetComponent<ConfigurableJointProperties>().strength=rootPositionSpring;
root.GetComponent<ConfigurableJointProperties>().strength=rootPositionSpring;
rightUpperArm.GetComponent<ConfigurableJointProperties>().strength=rightArmPositionSpring;
rightForearm.GetComponent<ConfigurableJointProperties>().strength=rightArmPositionSpring;
rightHand.GetComponent<ConfigurableJointProperties>().strength=rightArmPositionSpring;
leftUpperArm.GetComponent<ConfigurableJointProperties>().strength=rightArmPositionSpring;
leftForearm.GetComponent<ConfigurableJointProperties>().strength=rightArmPositionSpring;
leftHand.GetComponent<ConfigurableJointProperties>().strength=rightArmPositionSpring;
rightThigh.GetComponent<ConfigurableJointProperties>().strength=rightLegPositionSpring;
rightCalf.GetComponent<ConfigurableJointProperties>().strength=rightLegPositionSpring;
rightFoot.GetComponent<ConfigurableJointProperties>().strength=rightLegPositionSpring;
leftThigh.GetComponent<ConfigurableJointProperties>().strength=leftLegPositionSpring;
leftCalf.GetComponent<ConfigurableJointProperties>().strength=leftLegPositionSpring;
leftFoot.GetComponent<ConfigurableJointProperties>().strength=leftLegPositionSpring;
}
}
I just pop that script onto the root, assign the right objects to their corresponding slots and then I should be able to change the strength variable by group. This works. Kind of. The root/head/torsoSpringPosition variables all work as they should, however the limbs simply aren't responding. I figure it has something to do with the actual limbs because if I stick that script on a cube and assign that cube to the rightForearm slot, the script on the cube responds, but when the right forearm is assigned, nothing. The only difference between the limbs and the rest of the body is they use capsule colliders while the rest use box and spheres, and changing that didn't seem to change anything either. Thoughts?
Do you really need to reset the strength in every FixedUpdate step? Why/where does it ever change?
I haven't done ragdoll scripts but I did have a similar problem that my script didn't respond to some objects when it did to others. I found out that when I had duplicated some objects so the tag changing didn't help and they didn't work. Can you have this problem? Or can the na$$anonymous$$g of the object be the problem? if you have same names on different objects.
FixedUpdate was just me trying different stuff, had the same problem in Update. I'll look into duplicates, but the entire ragdoll is copy/pasted from the master, so if being a duplicate or having the same name could cause a problem, you'd think none of the ragdoll parts would respond but it's just the arms and legs. I have been doing alot of experimenting so maybe something got wonky in the process, I could just try setting it up from scratch, although I was hoping it was just a $$anonymous$$or oversight on my part.
EDIT: Started from scratch, just took my imported model, used the ragdoll wizard, put the scripts on, and it is still not working on the arms and legs. If you look away from the other stuff, I am basically just telling one script to set a variable on another script, I can not for the life of me figure out what is up with these limbs.
Answer by dallingtonp · Nov 26, 2014 at 03:13 AM
Well problem solved, turns out all the bodypart slots in the control script need to be filled and all those parts need to have the configurablejointproperties script attached, otherwise it throws me a null reference error and everything gets wonky. I hope we all learned a lesson today, null reference errors are scary, scary things.
Your answer
Follow this Question
Related Questions
Following Script Not Working Properly 0 Answers
GuiButton Going To Next Level 0 Answers
Script not working 0 Answers