- Home /
Question by
SteveHadley · Dec 29, 2017 at 12:37 AM ·
scripting problemraycastnullreferenceexception
RayCastHit.transform.gameObject.GetComponent works in editor but not in standalone build
I'm firing raycasts at objects in my scene and then using Component.SendMessage to let them know they have been hit. Pretty simple stuff and works fine in the editor however when I create a standalone build I get
NullReferenceException: Object reference not set to an instance of an object at ViveCursor.Update () [0x0006e] in D:\Dev\public\viveshooter\Assets\Scripts\ViveCursor.cs:134
The code that is failing is
void Update () {
Ray raycast = new Ray(transform.position, transform.forward);
RaycastHit hitObject;
// The script I am sending the "hit" message to which is attached to the gameobject I am shooting raycasts at
HitScript hitNode;
bool rayHit = Physics.Raycast(raycast, out hitObject);
if(Input.GetKeyDown(KeyCode.JoystickButton14) || Input.GetKeyDown(KeyCode.JoystickButton15) && rayHit && hitObject.transform.tag=="node"){
hitNode = hitObject.transform.gameObject.GetComponent<HitScript>();
hitNode.SendMessage("Hit");
}
float beamLength = GetBeamLength(rayHit, hitObject);
SetPointerTransform(beamLength, thickness);
}
Line 134 is hitNode = hitObject.transform.gameObject.GetComponent();
I can't work out why this works fine in the editor and then fails on a standalone build? Is there a different way I should be referencing that script and sending messages to it?
Comment