I'm currently working with Unity and an asset called OpenHatpics
. I want to generate an invisible wall that user is only able to go through it once, then disable its penetration. This asset has specific built in property for this penetration, but I need to reload the environment initialization.
The problem with this initialization is that when I try to reloading SetHapticGeometry
, it gives me a Null Reference Exception Error
. I've been through this error before and I think this is occurring because I'm not specifically referring to specific gameobject
but not entirely sure about how to fix this problem.
Here is the method that I'm calling from, which is attached to a GameObject
under tag GameController
.
using UnityEngine;
using System.Collections;
public class InstantiateByTouch : MonoBehaviour {
private GenericFunctionsClass GFC;
private GameObject invisibleWall;
private HapticProperties HP;
void Start () {
//Specifying which GameObject will be used
GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
GameObject invisibleWall = GameObject.Find ("Invisible Wall");
//Accessing components that will be used
if (gameControllerObject != null) {
GenericFunctionsClass GFC = gameControllerObject.GetComponent<GenericFunctionsClass> ();
}
HP = invisibleWall.GetComponent<HapticProperties> ();
}
void OnTriggerEnter (Collider collider) {
if (collider.gameObject.CompareTag("Player")) {
if (transform.parent.tag == "Touchable") {
HP.popThrough = 0.0f;
GFC.SetHapticGeometry (); //The problem occurs here
}
}
}
}
And SetHapticGeometry()
from GFC is following.
GameObject[] myObjects = GameObject.FindGameObjectsWithTag("Touchable") as GameObject[];
for (int ObjId = 0; ObjId < myObjects.Length; ObjId++)
{
/***************************************************************/
//Set the Transformation Matric of the Object
/***************************************************************/
//Get the Transformation matrix from object
Matrix4x4 m = new Matrix4x4();
//Build a transform Matrix from the translation/rotation and Scale parameters fo the object - Local Matrix
//m.SetTRS(myObjects[ObjId].transform.position,myObjects[ObjId].transform.rotation,myObjects[ObjId].transform.localScale);
//Build a transform Matrix from the translation/rotation and Scale parameters fo the object - Glabal Matrix
m = myObjects[ObjId].transform.localToWorldMatrix;
//Convert Matrix4x4 to double16
double[] matrix = ConverterClass.ConvertMatrix4x4ToDouble16(m);
//Convert Double16 To IntPtr
IntPtr dstDoublePtr = ConverterClass.ConvertDouble16ToIntPtr(matrix);
//Convert String to Byte[] (char* in C++) and Byte[] to IntPtr
IntPtr dstCharPtr = ConverterClass.ConvertStringToByteToIntPtr(myObjects[ObjId].name);
//Send the transformation Matrix of the object
PluginImport.SetObjectTransform(ObjId, dstCharPtr, dstDoublePtr);
/***************************************************************/
/***************************************************************/
//Set the Mesh of the Object
/***************************************************************/
//Get Mesh of Object
Mesh mesh = myObjects[ObjId].GetComponent<MeshFilter>().mesh;
Vector3[] vertices = mesh.vertices;
int[] triangles = mesh.triangles;
//Reorganize the Array
float[] verticesToSend = ConverterClass.ConvertVector3ArrayToFloatArray(vertices);
//Allocate Memory according to needed space for float* (3*4)
IntPtr dstVerticesArrayPtr = Marshal.AllocCoTaskMem(vertices.Length * 3 * Marshal.SizeOf(typeof(float)));
//Copy to dstPtr
Marshal.Copy(verticesToSend,0,dstVerticesArrayPtr,vertices.Length * 3);
//Convert Int[] to IntPtr
IntPtr dstTrianglesArrayPtr = ConverterClass.ConvertIntArrayToIntPtr(triangles);
//Send the Raw Mesh of the object - transformation are not applied on the Mesh vertices
PluginImport.SetObjectMesh(ObjId,dstVerticesArrayPtr, dstTrianglesArrayPtr,vertices.Length,triangles.Length);
/***************************************************************/
/***************************************************************/
//Get the haptic parameter configuration
/***************************************************************/
ReadHapticProperties(ObjId, myObjects[ObjId]);
/***************************************************************/
}
}