Faulty NullReferenceException?
I'm working on a music-based project and am trying to do the following initialization:
int currentFrame = rhythmTool.CurrentFrame;
And whenever I run my project, Unity throws an exception pointing directly to that line (it's the first line in Update()) and noting it as "NullReferenceException".
This happened only after Unity encountered a "fatal error" and had to be force-closed.
Hello, @Pengawk. For us to definitely resolve this problem, please copy and paste the script that contains this problem. Thanks!
Answer by jellythedonut · Feb 18, 2016 at 03:08 AM
A NullReferenceException typically refers to you trying to access a value which does not exist. So, for instance if you created an object and tried to get that object's value, but the object is not initialized, you'd get this exception. I am fairly certain that in the context of you question, this means that you didn't initialize the "rhythmTool". Do you have something like that sets the rhythmTool equal to something in your Start method?
Answer by EmHuynh · Feb 18, 2016 at 03:22 AM
Hi, @Pengawk. The problem is caused by not initializing rhythmTool
. You can set it by using the inspector (if its access modifier is public) or you can initialize it in the Start
function. There is not enough information regarding rhythmTool
, so this answer might not accurately solve the problem. However, by researching, I assume you are using RhythmTool. I found a documentation for it.
To solve this issue, initialize rhythmTool
:
public RhytmTool rhythmTool; // Can be set in the inspector by selecting a RhythmTool component.
void Start() {
// Initialize rhythmTool by getting the RhythmTool component of this object.
rhythmTool = GetComponent< RhythmTool >();
}
If the initialization of rhythmTool
failed, it is usually because GetComponent could not find a RhythmTool component.
I already have that in place.
public class BasicController : $$anonymous$$onoBehaviour
{
public RhythmTool rhythmTool;
public AudioClip audioClip;
Analysis mid;
public GameObject arrowUp;
Vector3 moveUp;
bool isCreated;
// Use this for initialization
void Start ()
{
rhythmTool = GetComponent<RhythmTool>();
rhythmTool.NewSong(audioClip);
mid = rhythmTool.$$anonymous$$id;
moveUp = new Vector3(0.65F, 5, 0);
isCreated = true;
}
Where the GameObject arrowUp is being assigned within Unity in the Inspector.
Hello, @Pengawk. Is there a RhythmTool component attached to the same game object as the BasicController script?
Indeed there is. I figured it out just a few moments ago. It had something to do with Unity and Visual Studio not cooperating after Unity crashed completely.
Your answer
Follow this Question
Related Questions
Having a issue with a NullReferenceException 1 Answer
Using TextMeshProUGUI variable in a static function is returning a NullReferenceException 0 Answers
List remains null after initialisation? 1 Answer
Null Reference in UnityStandardAssets.Utility.WaypointProgressTracker.Update 0 Answers
NullReferenceException when attempting to call a GameObject from within PointerEventData 1 Answer