- Home /
must be fully assigned before control leaves the constructor
hey, iam writing a small joint angular limit script that looks like this:
using System;
using UnityEngine;
namespace Physics
{
[Serializable]
//public class JointAngularLimits : MonoBehaviour
public struct JointAngularLimits : IPropertyBackingFieldCompatible, ICloneable, IPropertyBackingFieldCompatible<JointAngularLimits>, IEquatable<JointAngularLimits>
{
[SerializeField, PropertyBackingField]
private float m_XMin;
[PropertyBackingField, SerializeField]
private float m_XMax;
[SerializeField, PropertyBackingField]
private float m_YMax;
[SerializeField, PropertyBackingField]
private float m_ZMax;
public JointAngularLimits(float xMin, float xMax, float yMax, float zMax)
{
xMin = Mathf.Min(xMin, xMax);
xMax = Mathf.Max(xMin, xMax);
this.m_XMin = xMin;
this.m_XMax = xMax;
this.m_YMax = yMax;
this.m_ZMax = zMax;
}
public JointAngularLimits(Joint joint)
{
if (joint == null)
{
return;
}
if (joint is CharacterJoint)
{
CharacterJoint characterJoint = joint as CharacterJoint;
this.m_XMin = characterJoint.lowTwistLimit.limit;
this.m_XMax = characterJoint.highTwistLimit.limit;
this.m_YMax = characterJoint.swing1Limit.limit;
this.m_ZMax = characterJoint.swing2Limit.limit;
}
else if (joint is ConfigurableJoint)
{
ConfigurableJoint configurableJoint = joint as ConfigurableJoint;
this.m_XMin = configurableJoint.lowAngularXLimit.limit;
this.m_XMax = configurableJoint.highAngularXLimit.limit;
this.m_YMax = configurableJoint.angularYLimit.limit;
this.m_ZMax = configurableJoint.angularZLimit.limit;
}
else if (joint is HingeJoint)
{
HingeJoint hingeJoint = joint as HingeJoint;
this.m_XMin = hingeJoint.limits.min;
this.m_XMax = hingeJoint.limits.max;
}
}
public float XMax
{
get
{
return this.m_XMax;
}
private set
{
this.m_XMax = Mathf.Max(Mathf.Min(value, 180f), this.m_XMin);
}
}
public float XMin
{
get
{
return this.m_XMin;
}
private set
{
this.m_XMin = Mathf.Min(Mathf.Max(value, -180f), this.m_XMax);
}
}
public float YMax
{
get
{
return this.m_YMax;
}
set
{
this.m_YMax = Mathf.Clamp(value, 0f, 180f);
}
}
public float ZMax
{
get
{
return this.m_ZMax;
}
set
{
this.m_ZMax = Mathf.Clamp(value, 0f, 180f);
}
}
public void ApplyToJoint(Joint joint)
{
if (joint == null)
{
return;
}
if (joint is CharacterJoint)
{
CharacterJoint characterJoint = joint as CharacterJoint;
SoftJointLimit softJointLimit = characterJoint.lowTwistLimit;
softJointLimit.limit = this.XMin;
characterJoint.lowTwistLimit = softJointLimit;
softJointLimit = characterJoint.highTwistLimit;
softJointLimit.limit = this.XMax;
characterJoint.highTwistLimit = softJointLimit;
softJointLimit = characterJoint.swing1Limit;
softJointLimit.limit = this.YMax;
characterJoint.swing1Limit = softJointLimit;
softJointLimit = characterJoint.swing2Limit;
softJointLimit.limit = this.ZMax;
characterJoint.swing2Limit = softJointLimit;
}
else if (joint is ConfigurableJoint)
{
ConfigurableJoint configurableJoint = joint as ConfigurableJoint;
SoftJointLimit softJointLimit2 = configurableJoint.lowAngularXLimit;
softJointLimit2.limit = this.m_XMin;
configurableJoint.lowAngularXLimit = softJointLimit2;
softJointLimit2 = configurableJoint.highAngularXLimit;
softJointLimit2.limit = this.m_XMax;
configurableJoint.highAngularXLimit = softJointLimit2;
softJointLimit2 = configurableJoint.angularYLimit;
softJointLimit2.limit = this.m_YMax;
configurableJoint.angularYLimit = softJointLimit2;
softJointLimit2 = configurableJoint.angularZLimit;
softJointLimit2.limit = this.m_ZMax;
configurableJoint.angularZLimit = softJointLimit2;
}
else if (joint is HingeJoint)
{
HingeJoint hingeJoint = joint as HingeJoint;
JointLimits limits = hingeJoint.limits;
limits.min = this.m_XMin;
limits.max = this.m_XMax;
hingeJoint.limits = limits;
}
}
public object Clone()
{
return this;
}
public override bool Equals(object obj)
{
return ObjectX.Equals<JointAngularLimits>(ref this, obj);
}
public bool Equals(JointAngularLimits other)
{
return this.GetHashCode() == other.GetHashCode();
}
public override int GetHashCode()
{
return ObjectX.GenerateHashCode(new int[]
{
this.m_XMax.GetHashCode(),
this.m_XMin.GetHashCode(),
this.m_YMax.GetHashCode(),
this.m_ZMax.GetHashCode()
});
}
public int GetSerializedPropertiesHash()
{
return this.GetHashCode();
}
public override string ToString()
{
return string.Format("[JointAngularLimits: XMin={0}, XMax={1}, YMax={2}, ZMax={3}]", new object[]
{
this.m_XMin,
this.m_XMax,
this.m_YMax,
this.m_ZMax
});
}
}
}
but now iam getting these errors: JointAngularLimits.m_XMin' must be fully assigned before control leaves the constructor
JointAngularLimits.m_XMax' must be fully assigned before control leaves the constructor
JointAngularLimits.m_YMax' must be fully assigned before control leaves the constructor
JointAngularLimits.m_ZMax' must be fully assigned before control leaves the constructor
JointAngularLimits.m_XMin' must be fully assigned before control leaves the constructor
any idea why?
Answer by merkaba48 · Jul 05, 2017 at 12:43 PM
In all code paths in your constructors, be sure to assign those vars to a value even if you don't need to. At least, that's what I've had to do when encountering this error. I'm not sure why you have to in your case, as I thought floats automatically initialized to 0.
Try setting the variables to 0 where you declare them in the class body, if that doesn't work, try doing it at the beginning of the constructor with the if
clauses, otherwise, I'm not sure.
thnaks, declare them before the if with just this.m_X$$anonymous$$in = 0; worked out :)
Your answer
Follow this Question
Related Questions
Hinge joint limits not working. 2 Answers
Hinge Joint Limits in C sharp 1 Answer
How to set a different linear limit for each axis on a configurable joint 0 Answers
Tie objects together 5 Answers
How do I see a Configurable Joint in the Scene Window? 0 Answers