Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 mrt · Oct 13, 2011 at 03:11 PM · androidnullreferenceexception

Script works on some objects and not others, also varies between scenes...

I have made a script that discovers where the user press. Three gameobjects are placed on top of a ImageTarget. The strange thing is that the script works on some gameobjects and not others. I have six scenes with the exact same game objects (called Text, Image and Puzzle). Each object is marked with "is trigger". In one scene everything works, in the next only Puzzle, in the third only Text and Image. The script is attached to the Imagetarget. The error seems to be a "NullReferenceException" for filename unknown... I have sent with my LogCat:

 10-12 20:57:34.075: INFO/Unity(1942): TouchPhase.Began
 10-12 20:57:34.075: INFO/Unity(1942): UnityEngine.Debug:Internal_Log(Int32, String, Object)
 10-12 20:57:34.075: INFO/Unity(1942): UnityEngine.Debug:Log(Object)
 10-12 20:57:34.075: INFO/Unity(1942): UnityEngine.MonoBehaviour:print(Object)
 10-12 20:57:34.075: INFO/Unity(1942): GameObjectButton:FixedUpdate()
 10-12 20:57:34.075: INFO/Unity(1942):  
 10-12 20:57:34.075: INFO/Unity(1942): (Filename: /Applications/buildAgent/work/842f9557127e852/Runtime/Export/Generated/UnityEngineDebug.cpp Line: 34)
 10-12 20:57:34.085: INFO/Unity(1942): Start PressedObject
 10-12 20:57:34.085: INFO/Unity(1942): UnityEngine.Debug:Internal_Log(Int32, String, Object)
 10-12 20:57:34.085: INFO/Unity(1942): UnityEngine.Debug:Log(Object)
 10-12 20:57:34.085: INFO/Unity(1942): UnityEngine.MonoBehaviour:print(Object)
 10-12 20:57:34.085: INFO/Unity(1942): GameObjectButton:PressedObject(Vector3)
 10-12 20:57:34.085: INFO/Unity(1942): GameObjectButton:FixedUpdate()
 10-12 20:57:34.085: INFO/Unity(1942):  
 10-12 20:57:34.115: INFO/Unity(1942): (Filename: /Applications/buildAgent/work/842f9557127e852/Runtime/Export/Generated/UnityEngineDebug.cpp Line: 34)
 10-12 20:57:34.125: INFO/Unity(1942):  closestObject.name = Puzzle
 10-12 20:57:34.125: INFO/Unity(1942): UnityEngine.Debug:Internal_Log(Int32, String, Object)
 10-12 20:57:34.125: INFO/Unity(1942): UnityEngine.Debug:Log(Object)
 10-12 20:57:34.125: INFO/Unity(1942): UnityEngine.MonoBehaviour:print(Object)
 10-12 20:57:34.125: INFO/Unity(1942): GameObjectButton:FixedUpdate()
 10-12 20:57:34.125: INFO/Unity(1942):  
 10-12 20:57:34.125: INFO/Unity(1942): (Filename: /Applications/buildAgent/work/842f9557127e852/Runtime/Export/Generated/UnityEngineDebug.cpp Line: 34)
 10-12 20:57:34.135: INFO/Unity(1942): NullReferenceException: Object reference not set to an instance of an object
 10-12 20:57:34.135: INFO/Unity(1942):   at GameObjectButton.FixedUpdate () [0x00000] in <filename unknown>:0 
 10-12 20:57:34.135: INFO/Unity(1942):  
 10-12 20:57:34.135: INFO/Unity(1942): (Filename:  Line: -1)

In the inspector i choose ImageTarget as my Gameobject for obj. My C# code:

 using UnityEngine;
 using System.Collections;
 
 public class GameObjectButton : MonoBehaviour {
     public GameObject obj;
     private GameObject[] GameObj = new GameObject[3];
     Transform objPosition = null;
     private GameObject closest;    
     
     void Start()
     {
         //Fill upp GameObj array
         GameObject text = GameObject.Find("Text");
         GameObject puzzle = GameObject.Find("Puzzle");
         GameObject image = GameObject.Find("Image");
         
         GameObj[0] = text;
         GameObj[1] = puzzle;
         GameObj[2] = image;
     }
     
     void Awake()
     {
         Input.multiTouchEnabled = false;
 
         if (obj)
         {
             objPosition = obj.transform;
             print("Object was transformed");
         }
         else
         {
             GameObjectButton script = GetComponent<GameObjectButton>();
             script.enabled = false;
             print("Script was disabled");
         }
     }
 
 
     void FixedUpdate()
     {
         if (obj)
         {
             foreach (Touch touch in Input.touches)
             {
                 Ray ray = Camera.main.ScreenPointToRay(touch.position);
                 RaycastHit hit;
 
                 if (touch.phase == TouchPhase.Began)
                 {
                     
                     print("TouchPhase.Began\n"); 
                     
                     if (Physics.Raycast(ray, out hit))
                     {
                         GameObject closestObject = PressedObject(hit.point);
                         print("****************\n" + hit.point + "\n***************\n" + objPosition);
                         print("\n closestObject.name = " + closestObject.name);
                         
                         var jc = new AndroidJavaClass("com.company.project.UnityPlay");
                         print(jc);
                         var jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
                         print(jo);
                         jo.Call("Launch", closestObject.name);
                     }
                 }
             }
         }
     }
     
     GameObject PressedObject(Vector3 dist){
         print("Start PressedObject");
         float distance = Mathf.Infinity;
         foreach (GameObject go in GameObj){
             Vector3 diff = go.transform.position - dist;
             print(go.name + " difference: " + diff);
             float curDistance = diff.sqrMagnitude;
             print(curDistance);
             if(curDistance < distance){
                 closest = go;
                 distance = curDistance;
                 print("Closest: " + closest.name);
             }
         }
         print("Return value: " + closest);
         return closest;
     }
 }

The C# calls a Java file that has a intent that is supposed to start the next event. In onCreate the intents are created. (I do not believe the problem is located here) Java code:

 public class UnityPlay extends UnityPlayerActivity{
   
     public void Launch(String stringType){
         DebugLog.LOGD("Launch(type) started, type value: " + stringType);
         DebugLog.LOGD("stringType value = \"" + stringType + "\" ");
         
         NobelApplication nobelApp = ((NobelApplication)getApplicationContext());
         place = nobelApp.getPlace();
         nobelApp.setWhereFrom("UnityPlay");
         
         //Checks the hasCode of the incoming type to load the right view.
         if (stringType.hashCode() == "Puzzle".hashCode()){
             DebugLog.LOGD("Puzzle Activated");
             //Sets status for that location
             place.setStatus(1);
             initAndOpenDatabase();
             myDbHelper.updateStatusForPlace(place, place.getStatus());
             startActivity(load_Puzzle);
         }else if(stringType.hashCode() == "Image".hashCode()){
             DebugLog.LOGD("Image Activated");
             startActivity(load_Image);
         }else if(stringType.hashCode() == "Text".hashCode()){
             DebugLog.LOGD("Text Activated");
             startActivity(load_Text);
         }else{
             DebugLog.LOGD("Object not found");
         }
     }
 }


I have been looking around without success for what might cause this problem. Since it is working for some objects and not others I am considering if it might be a bug? Would it be better to create three scripts (one for each game object) and that would solve the problem? Any one have a clue?

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

0 Replies

· Add your reply
  • Sort: 

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.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

NullReferenceException: Object reference not set to an instance of an object 3 Answers

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

Building for android freezes unity editor 2 Answers

itween example work well on unity3d while NullReferenceException on android 0 Answers

Unity3D Android NullReferenceException for class properties during using Input.location.lastData 2 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