- Home /
NullReferenceException
This may be a simple fix due to the error being thrown is a NullReferenceException. I am unable though to find where the actual error is in the code where the compiler thinks the error occurs. The two Scripts that are being used are from STRING Augmented Reality so the C# scripts are a little new to me.
I have marked the error with Asterisks to show the line of code where the error is.
Hopefully someone here can help! Thanks
public class DrawingUI : MonoBehaviour {
public Camera mainCamera;
public Camera stringCamera;
private bool isMainCameraEnabled = false;
public bool isStringCameraEnabled = false;
// Use this for initialization
void Start () {
mainCamera.enabled = true;
isMainCameraEnabled = true;
isStringCameraEnabled = false;
stringCamera.enabled = false;
}
// Update is called once per frame
void Update () {
if(isMainCameraEnabled == true)
{
mainCamera.enabled = true;
stringCamera.enabled = false;
}
if(isStringCameraEnabled == true)
{
stringCamera.enabled = true;
mainCamera.enabled = false;
}
}
void OnGUI(){
infoWindow = GUI.Window(1,materialsWindow, materialsWindowFunc, "Material Window");
GUILayout.BeginArea(new Rect (Screen.width / 2 - backButtonWidth /2, Screen.height / 2 - backButtonHeight/2 + 900 - backButtonSpacing,
backButtonWidth, backButtonHeight));
if(GUILayout.Button("View Model", GUILayout.Height(backButtonSpacing)))
{
isStringCameraEnabled = true;
isMainCameraEnabled = false;
cameraCentricARManager.StartString();
//Application.LoadLevel("StringUI");
}
if(GUILayout.Button("Back to Main Menu", GUILayout.Height(backButtonSpacing)))
{
Application.LoadLevel("MainMenuUI");
}
GUILayout.EndArea();
}
public class CameraCentricARManager : MonoBehaviour {
public GameObject[] rootObjects;
public bool fullscreen = false;
public float alignment = 0.5f;
public bool reorientBranding = true;
float[] lastSpottedTimes;
public StringWrapper stringWrapper;
public void StartString()
{
// Initialize String
stringWrapper = new StringWrapper(null, camera, reorientBranding, fullscreen, alignment);
// Load some image targets
for (uint i = 0; i < rootObjects.Length; i++)
{
stringWrapper.LoadImageMarker("Marker " + (i + 1), "png");
}
// Hide all rootObjects
// Also, set them as children of the camera;
// This is to more easily position them relative to the camera later
for (uint i = 0; i < rootObjects.Length; i++)
{
rootObjects[i].SetActiveRecursively(false);
rootObjects[i].transform.parent = transform;
}
// Allocate array to track the last time each marker was spotted
lastSpottedTimes = new float[rootObjects.Length];
// Prevent the iOS keyboard from introducing an unwanted
// black frame when rotating
iPhoneKeyboard.autorotateToLandscapeLeft = false;
iPhoneKeyboard.autorotateToLandscapeRight = false;
iPhoneKeyboard.autorotateToPortrait = false;
iPhoneKeyboard.autorotateToPortraitUpsideDown = false;
}
void CreateWrapper()
{
******** //Error occurs here, Located in StringWrapper Update Method ***********
// Perform marker image tracking
uint markerCount = stringWrapper.Update();
// Handle detected markers
for (uint i = 0; i < markerCount; i++)
{
// Fetch tracker data for this marker
StringWrapper.MarkerInfo markerInfo = stringWrapper.GetDetectedMarkerInfo(i);
if (markerInfo.imageID < rootObjects.Length && rootObjects[markerInfo.imageID] != null)
{
rootObjects[markerInfo.imageID].transform.localPosition = markerInfo.position;
rootObjects[markerInfo.imageID].transform.localRotation = markerInfo.rotation;
lastSpottedTimes[markerInfo.imageID] = Time.time;
if (!rootObjects[markerInfo.imageID].active)
{
rootObjects[markerInfo.imageID].SetActiveRecursively(true);
}
}
}
// Deactivate rootObjects for lost markers
for (uint i = 0; i < rootObjects.Length; i++)
{
if (rootObjects[i].active)
{
if (Time.time - lastSpottedTimes[i] > 2)
{
// Marker has been out of view for a while; Deactivate it
rootObjects[i].SetActiveRecursively(false);
}
else if (Time.time != lastSpottedTimes[i])
{
// Marker wasn't spotted this frame; Hide it
rootObjects[i].transform.localPosition = new Vector3(1000000, 0, 0);
}
}
}
}
}
This is the method from StringWrapper where the error happens
public uint Update()
{
if (wasInitialized)
{
if (isMobile)
{
if (Screen.orientation != currentOrientation && Time.frameCount > 1)
{
ApplyViewSettings();
MobileWrapper.String_SetProjectionAndViewport(_camera.projectionMatrix, _camera.rect, (int)(currentOrientation = Screen.orientation), _reorientBranding);
}
markerCount = MobileWrapper.String_GetData(markerInfo, maxMarkerCount);
}
else
{
if (DesktopWrapper.IsNewFrameReady())
{
DesktopWrapper.ProcessFrame((uint)videoTexture.GetNativeTextureID(), 0);
markerCount = DesktopWrapper.GetDataQuaternionBased(markerInfo, maxMarkerCount);
videoPlaneObject.active = true;
}
}
}
else
{
// Write dummy output data
markerCount = 1;
markerInfo[0].DummyData();
}
return markerCount;
}
Answer by DaveA · Sep 04, 2012 at 04:33 PM
When is StartString called? My guess is either never, or after this Update function is called. Make sure it is called before that Update. Either call it Start or call it FROM Start, or edit the script execution order to put that class first.
Thanks for the response Dave, I realized what I was doing wrong. I did not put my third script in thinking it was not a factor but it seems it is. I am going to update my code above with the 3rd script! Thanks
An Update as well, the problem is no longer with Null Exception, that was fixed by rena$$anonymous$$g the Update method CreateWrapper
The problem now is that the actual String Camera will not function.