• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by djkr · Sep 05, 2011 at 10:34 PM · nullreferenceexception

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[i] = 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");
                     }

}

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by aldonaletto · Sep 06, 2011 at 12:15 AM

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.
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image djkr · Sep 06, 2011 at 12:29 AM 0
Share

Thanks aldonaletto... but the original code before this version...

I did not have: if (touch == null) { //nothin
} ..but still had the error... I read from another ReferenceNull problem Question, that if you put in a NULL, then it should be fine.. when I tried it, it still had no effect... (I'm talking about the answer I found here:) http://answers.unity3d.com/questions/27678/nullreferenceexception-object-reference-not-set-to.html

Any other suggestions? DJ$$anonymous$$R

avatar image aldonaletto · Sep 06, 2011 at 12:57 AM 0
Share

The problem isn't writing if (touch==null) - this works the same as if (!touch). The problem I pointed out is that this if doesn't avoid the code below it to be executed when touch is null! If touch is null, you will get the null reference error in this line:

pos = Vector3(touch.position.x, Screen.height - touch.position.y, 0.0);
To avoid this, you could write this if like in my answer, or just add an else { after your if :

       if (touch == null) { 
         //nothin  
       } else { // <- this else must be added
         pos = Vector3(touch.position.x, Screen.height - touch.position.y, 0.0);
         ...
       } // <- don't forget to close the else clause with this brace
       ...
avatar image
0

Answer by djkr · Sep 06, 2011 at 01:48 PM

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SisterKy · Sep 13, 2011 at 07:43 PM 0
Share

please don't post comments as answers to your question. use the 'add new comment'-button.

avatar image
0

Answer by djkr · Sep 13, 2011 at 07:35 PM

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SisterKy · Sep 13, 2011 at 07:44 PM 0
Share

if this is 'the answer' to this question, please click the little checkmark top left of this post (by the thumbs)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException...Camera problem??? 1 Answer

NullReferenceException: Object reference not set to an instance of an object 1 Answer

Help On error Please 1 Answer

For Loop and Null referance error Ios Scripting 1 Answer

Using iTween with a prefab tree. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges