|
I'm working through a tutorial that uses a guitexture for a lifebar for the player and mobs. I keep getting a null reference for the player and all the mobs in the scene. The error doesn't make sense: NullReferenceException UnityEngine.GUITexture.get_pixelInset () (at C:/BuildAgent/work/d9c061b1c154f5ae/Runtime/ExportGenerated/Editor/Graphics.cs:3187) myGUI.Start () (at Assets/Scripts/HUD Classes/myGUI.cs:112) While the private variable is called _display, there is nothing with an underscore for pixcelInset.
(comments are locked)
|
abouti publish this as a second answer cause it too big for comment 8) NullReferenceException is rather the most popular error in unity. it just says that you have somewhere null (read: you haven't an object or you have nothing), but trying to call something for this object. of course, cause you havn't object, you can't do something with it. for example, i want to set some speed to Rigidbody component of some gameobject. simply i will do it so: myGameObject.rigidbody.velocity = Vector3.forward * 10f; but, it's not safe cause an object here can absent. it can absent cause i forget to attach it or it was but is destroyed now. so, to be sure i should check it first: if (myGameObject != null)
{
myGameObject.rigidbody.velocity = Vector3.forward * 10f;
}
else
{
...do something when no object to attach speed
}now i will not call to rigidbody if i have no gameObject. but, i still can get NullReferenceException, cause myGameObject can be without rigidbody, and myGameObject.rigidbody will return null. in this case myGameObject.rigidbody.velocity will raise error. so: if (myGameObject != null)
{
if (myGameObject.rigidbody == null)
{
myGameObject.AddComponent<Rigidbody>();
}
//at this point i absolutely sure that myGameObject is exists (checked it)
//and it has a rigidbody (it was on GameObject or just created if wasn't)
//so, now my call to velocity is safety
myGameObject.rigidbody.velocity = Vector3.forward * 10f;
}
else
{
...do something when no object to attach speed
}
debugtask: we have some line of code with NullReferenceException. what's wrong? answer: for example, our line of code is: myGameObject.rigidbody.velocity = Vector3.forward * 10f; let's try to find null. insert step-by-step check using debug.log like here: Debug.Log(myGameObject); Debug.Log(myGameObject.rigidbody); Debug.Log(myGameObject.rigidbody.velocity); myGameObject.rigidbody.velocity = Vector3.forward * 10f; now, if we got null on first debug.log, it means we have no gameobject. go to code above and look why this happens if we got some gameobject in first debug but null in second, it means our gameobject somewhy has no rigidbody if second debug shows component, third debug will show speed 8) it can't be null cause rigidbody always has speed I did as you suggested and debugged out all the parts of the assignment. Display is: TestplayerLifebar (UnityEngine.GUITexture) UnityEngine.Debug:Log(Object) myGUI:Awake() (at Assets/Scripts/HUD Classes/myGUI.cs:102) Bar length Max is (left:-64.00, top:-29.00, width:128.00, height:15.00) UnityEngine.Debug:Log(Object) myGUI:Awake() (at Assets/Scripts/HUD Classes/myGUI.cs:104) Bar length Max is 128 UnityEngine.Debug:Log(Object) myGUI:Awake() (at Assets/Scripts/HUD Classes/myGUI.cs:105) They all come out fine, but I'm still getting the null reference. Any other thoughts? Attaching to the player did not work either. Something else is clobbering it. Although, just before the null reference there is a debug statement for _display, but I don't know where it's being called from.
Aug 17 '12 at 11:06 PM
Gilead7
debug _display variable. something IS null if you got NullRefeenceException. there can't be 'all come out fine'.
Aug 18 '12 at 09:34 PM
ScroodgeM
(comments are locked)
|
|
error points you directly to line where error occurs: myGUI.Start () (at Assets/Scripts/HUD Classes/myGUI.cs:112) this is method Start(), that you'd not publish. look for line 112 in myGUI.cs publish whole method Start() if need explained. Moved it out from start to awake, since the other declaration was in there. NullReferenceException: Object reference not set to an instance of an object myGUI.Awake () (at Assets/Scripts/HUD Classes/myGUI.cs:103) Still getting a null reference, even though it debugs out properly. _display=gameObject.GetComponent(); Debug.Log("Display is: "+_display); _maxBarLength=(int)_display.pixelInset.width; Debug.Log ("Bar length Max is "+_maxBarLength); Right now it's tied to a gui object onscreen, but it's not attached to anything else. Does it need to be attached to the player?
Aug 17 '12 at 02:13 PM
Gilead7
(comments are locked)
|
