- Home /
Null exception for public variable
Hello! I have been trying to get one script to read input from another. I have this script so far:
The sender script:
using UnityEngine;
using System.Collections;
public class Test: MonoBehaviour
{
public jumpAfterTest _jumpAfterTest;
void OnMouseDown()
{
Debug.Log ("Mouse Down");
_jumpAfterTest.JAT ();
}
void OnMouseUp()
{
Debug.Log ("Mouse up");
}
}
And the receiver script:
using UnityEngine;
using System.Collections;
public class jumpAfterTest : MonoBehaviour
{
public void JAT()
{
Debug.Log("Me is JAT");
}
}
The error I am getting is:
NullReferenceException: Object reference not set to an instance of an object Test.OnMouseDown () (at Assets/my-scripts/Test.cs:13) UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32, Int32)
I am guessing that this error has to do with line 13 of test script: _jumpAfterTest.JAT ();
How would I fix this? Thanks for the help! I am in mobile, by the way.
Answer by nullgobz · Aug 02, 2015 at 01:44 PM
Did you set the script in the inspector?
Click the object that has the Test script component attatched. Then drag and drop the gameobject that has the jumpAfterTest attatched to the public _jumpAfterTest slot.
Gosh, sorry about that. I read over the directions to quickly in the unity wiki I was reading. Thanks!
Answer by moriggi · Aug 02, 2015 at 06:25 PM
Hello in the Test script add this code in the start function _jumpAfterTest = GetComponent<_jumpAfterTest>(); This should work if the receiver script is in another GameObject.
Your answer
Follow this Question
Related Questions
unity3d www download of large texture mobile 0 Answers
Can I enable null reference checking in production code on iOS 1 Answer
IL2CPP does not support marshaling delegates that point to instance methods to native code. 1 Answer
Problem Saving game progress 1 Answer
Button issues since Catalina 0 Answers