NullReferenceException Reference Error - Please HELP! / How to fix?

Hi… I can not understand why I get this error:

NullReferenceException: Object reference not set to an instance of an object
UnityScript.Lang.Extensions.get_length (System.String s)
SwipeControl.Update () (at Assets/Scripts/SwipeControl.js:192)

The code is just a minor modification of the “SwipeControl.js” by Markus Hofer.

I have it go to a button script that checks the link associated with the button, then refreshes the page wit the new page.

Here is part of the code - I can post more if you like…
I can’t figure out if its the subroutine " () ", or the code just after it, with the “touch”…
My error says its the “touch” that is the source of the error message…

PLEASE HELP!.. This is holding up a manjor release for my company!.. Thank-UUUUUUUU!!!

					for(i = 1; i < prevSmoothValue.Length; i++) {
						prevSmoothValue *= prevSmoothValue[i- 1];*
  •   			}*
    
  •   			prevSmoothValue[0] = smoothValue;*
    
  •   		}*
    
  •   		grablink0 () ;*
    
  •   	#if UNITY_IPHONE or UNITY_ANDROID*
    
  •   	for(touch in Input.touches){*
    
  •   	if (touch == null) {*
    
  •   	//nothin	*
    
  •   	}*
    
  •   		pos = Vector3(touch.position.x, Screen.height - touch.position.y, 0.0);*
    
  •   		tPos = matrix.inverse.MultiplyPoint3x4(pos);		*
    
  •   		//BEGAN*
    
  •   		print(tPos + " inside " + mouseRect + "?");*
    
  •   		if (touch.phase == TouchPhase.Began && mouseRect.Contains(tPos)) {*
    
  •   			fingerStartArea[touch.fingerId] = 1;*
    
  •   			print("hit!");*
    
  •   		}*
    
  •   		//WHILE FINGER DOWN*
    
  •   		if(fingerStartArea[touch.fingerId] == 1) { // no touchRect.Contains check because once you touched down you're allowed to drag outside...*
    
  •   			touched = true;*
    
  •   			//START*
    
  •   			if(touch.phase == TouchPhase.Began) {*
    

_ smoothStartPos = smoothValue + tPos.x * partFactor;_

  •   				FillArrayWithValue(prevSmoothValue, smoothValue);*
    
  •   			}*
    


HERE IS THE BUTTONS LINK SCRIPT IT JUMPS TO:
function grablink0() {

  • // Code by Kris Roebuck of HangarApps / The App Hangar (A Division of MyFutureClub [USA])*
  •   						//grab value of button being pressed*
    
  •   					var code_b = linkb;*
    
  •  					print (code_b);* 
    

// Goes to the link
screen1_b = WWW(code_b);

*yield screen1_b; // Wait *
// ANY ERRORS?
if (screen1_b.error){
print("Error: "+screen1_b.error);

  •   TextHints.message = "ERROR (CHECK INTERNET CONNECTION)";*
    
  •   TextHints.textOn = true;*
    

} else {

  • var screenimb = (screen1_b.text);*
    // assign text
    *linkto = screenimb; *

  •   				if (linkto.length == 0) {*
    
  •   						///nothing*
    
  •   				}* 
    
  •   				if (linkto.length >= 12) {*
    
  •   						///webload web-page*
    
  •   				}* 
    
  •   				if (linkto.length <= 5) {*
    
  •   				loadkey.key = linkto;*
    
  •   				Application.LoadLevel ("VIEWER");*
    
  •   				}*
    

}

}

Based on the code snipets you supplied, it looks for me that you can have problems in this part:

       for(touch in Input.touches){
       if (touch == null) { // this if always does nothing 
       //nothin  
       }
       // this part of the code will be executed even if touch is null,
       // what will produce the null reference error
         pos = Vector3(touch.position.x, Screen.height - touch.position.y, 0.0);
         tPos = matrix.inverse.MultiplyPoint3x4(pos);   

As I said in my comment, it doesn’t matter if touch is null or not: the code after the if will be executed anyway. It would be better to do something like this:

       if (touch) {
         pos = Vector3(touch.position.x, Screen.height - touch.position.y, 0.0);
         ...

So the code only would be executed if touch were not null.

OK! Thank You VERY MUCH!
Your suggestion worked! I also recommend to anyone with this problem to try either saving, then quiting, and reloading everything, or to simply stop and play your scene a couple of times…strangely, it can also shake off the Null problem.

Found the Solution:

Thanks for your replies!.. I ended up creating a prefab that I could drag and drop into the Variable that had the Null (within Unity 3D).

Again, I appreciate your responses!

Cheers,
Kris