- Home /
NullReferenceException: Object reference not set to an instance of an object
Hi,
I am confused why I am getting this error. I have a fresh project with 2 scripts I found from http://wiki.unity3d.com/index.php/EditorAnimationCurveExtension CurveExtension.cs & KeyframeUtil.cs
I have 1 cube and 1 sphere in my scene with the following script attached to it. I am trying to move the keys frames of an existing curve. And this script does exactly that;
using UnityEngine;
using System.Collections;
using CurveExtended;
public class Test : MonoBehaviour
{
public AnimationCurve aggressionCurve;
public float randomMinKey;
public float randomMaxKey;
public float input = -2f;
public float output;
void Start ()
{
randomMinKey = (float)Random.Range(-100, -20);
randomMaxKey = (float)Random.Range(20, 80);
Keyframe[] myKey = aggressionCurve.keys;
myKey[0].time = randomMinKey;
myKey[1].time = randomMaxKey;
aggressionCurve.keys = myKey;
aggressionCurve.UpdateAllLinearTangents();
output = aggressionCurve.Evaluate(input);
Debug.Log("name - " + gameObject + ", input - " + input + ", output - " + output);
}
}
The strange part is EVERYTHING runs smoothly in editor & game mode without any warnings or errors.
When I build it, I get the error "NullReferenceException: Object reference not set to an instance of an object" as seen in the picture below:
I know the problem is linked to the line because when I comment out the line: aggressionCurve.UpdateAllLinearTangents();
in my Test.cs script everything builds nicely without errors but my curve is all messed up which is what that line fixes.
Does anyone have any ideas?
Thanks,
When you play in editor, does this error appears? If so, it should say the EXACTLY line that is giving you that error.
But, from what I can see, the problem is in those 2 lines:
aggressionCurve.keys = my$$anonymous$$ey;
aggressionCurve.UpdateAllLinearTangents();
Why? It shows 2 null exception, also, so are you referencing the AnimationCurve in the inspector? Double check that.
In editor (with play mode on) I do not get any warnings or errors. It only shows up when I build.
The reason I get 2 null errors is because I have the script on the 2 objects (the cube + sphere). If I get rid of 1 of the objects; I get 1 null reference (in build) ins$$anonymous$$d.
I thought the line: aggressionCurve.InsertSomethinghere() is referencing my curve.
I see that the link you posted above is for an Editor Extension... $$anonymous$$aybe you are using something from the UnityEditor namespace? in builds, everything inside a folder named Editor, and every UnityEditor namespace is ignored.
No I did not put anything in an editor folder.
"Place following scripts somewhere in your Assets folder under your Poroject"
I am only using CurveExtension.cs & $$anonymous$$eyframeUtil.cs
The last part is not mandatory. It is an example.
The script allows you to set the left and right tangents of keys to linear through script.
Still looks strange the way you are using that script... After all, after some reading, it looks like a script to create animations, something that isn't needed in builds. If that null exceptions aren't causing any troubles, them just ignore them, just be sure that you are using those scripts correctly.
Answer by maccabbe · Jan 23, 2016 at 03:29 AM
The problem is that EditorAnimationCurveExtension only works in the editor and I don't see a way to make it work in a build. Thanks to your well written question, I reproduced the error. I can walk you through how I narrowed it down and what is happening.
I ran a standalone build of the project and looked at output_log.txt in the standalone's _Data folder. The full error was
NullReferenceException: Object reference not set to an instance of an object
at CurveExtended.KeyframeUtil.GetKeyTangentMode (Keyframe keyframe, Int32 leftRight) [0x00000] in <filename unknown>:0
at CurveExtended.CurveExtension.UpdateTangentsFromMode (UnityEngine.AnimationCurve curve, Int32 index) [0x00000] in <filename unknown>:0
at CurveExtended.CurveExtension.UpdateAllLinearTangents (UnityEngine.AnimationCurve curve) [0x00000] in <filename unknown>:0
at Test.Start () [0x00000] in <filename unknown>:0
Which narrowed the issue down to code in KeyframeUtil.GetKeyTangentMode(Keyframe keyframe, int leftRight) which is part of EditorAnimationCurveExtension.
// UnityEditor.CurveUtility.cs (c) Unity Technologies
public static TangentMode GetKeyTangentMode(Keyframe keyframe, int leftRight)
{
Type t = typeof(UnityEngine.Keyframe);
Debug.Log("t "+(t == null));
FieldInfo field = t.GetField("m_TangentMode", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
Debug.Log("Field "+(field == null));
int tangentMode = (int)field.GetValue(keyframe);
if (leftRight == 0)
return (TangentMode)((tangentMode & 6) >> 1);
else
return (TangentMode)((tangentMode & 24) >> 3);
}
Then I added two debug logs to check the only 2 things used in this method that are classes, t and field and unsurprisingly field was null (t was not). The issue is that the line
FieldInfo field = t.GetField("m_TangentMode", BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);
does not work in standalone.
The field KeyFrame.m_TangentMode is a private field in a sealed class so it can only be accessed in this very hacky way. In editor this isn't a problem because KeyFrame.m_TangentMode exists in the editor. Since you are using t.GetField(string, BindingFlags) the compiler does not process "m_TangentMode" like it would if you could access the field normally. However when you build a Unity project editor only stuff is removed and code that isn't accessible from your project is stripped so KeyFrame.m_TangentMode no longer exists and this function no longer works.
Thank you for the great explanation. Now I understand.
I hope others that stumble upon that script find this thread. To avoid frustrations. I will post if I find any ways to successfully achieve the same results in build.
Answer by leeholl · Jan 23, 2016 at 01:31 AM
You should add a check to see if aggressionCurve is null before using it. This script assumes this is defined in the Inspector by assigning an animation clip, but if that is not the case, then you would referencing a null object. Add this as the first line of your Start method and see if it catches the issue:
if (aggressionCurve == null)
{
Debug.LogError(gameObject.name + ": AnimationCurve is undefined.");
return;
}
Your answer

Follow this Question
Related Questions
Accessing Animation Curves/Keyframes during runtime 1 Answer
key count: 1 on curve 'curvename' error 1 Answer
How to animate properties stored in arrays? 1 Answer
How am I supposed to use Mecanim system to animate the new UI from version 4.6? 1 Answer
Unity 5.5: Jump to Frame in Unity Animation Window 0 Answers