- Home /
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?
Your answer
Follow this Question
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