0

When I have this line of code inside fixed update I get an error in Unity:

if (active.transform.position.x <= -4)
{
    Debug.Log(active.transform.position.x.ToString());
}

NullReferenceException: Object reference not set to an instance of an object Handler.FixedUpdate () (at Assets/Handler.cs:79)

When used inside lateupdate however, I do not receive an error.

Why would it do this? I am trying to move a RigidBody using FixedUpdate with

if (active.transform.position.x <= -4)

as a parameter.

My question is why am i getting a null exception inside fixedupdate but not lateupdate. I know what a null exception is.

Daniel
  • 85
  • 7
  • Well what's null? `active`? `transform`? `position`? – AustinWBryan May 31 '18 at 01:55
  • I checked them all and none of them are. My code even works with the null exception. My question is why am i getting a null exception inside fixedupdate but not lateupdate. I know what a null exception is. – Daniel May 31 '18 at 02:08
  • 2
    It's not possible that none of them are null while still getting the NRE. Doing things like `active.transform.position.x` makes it difficult for you do debug your code. That's 4 layers deep into an object, – ChiefTwoPencils May 31 '18 at 02:18
  • Here's a tip of debbuging a `NullReference Exception` before referencing a variable to another object. For example `string str = "";` then you can reference `str` now something like this for example you have a 2D array `str = table[xIndex,yIndex];` like that – Ginxxx May 31 '18 at 06:17
  • The object is active, it has ttansform attatched, i have set the position. When i use this code in late update it works fine with no errors. When i use it in fixed update it runs fine with that one error – Daniel May 31 '18 at 12:06
  • Given that `transform`, `position`, and `x` are value-types, the issue must be that `active` is somehow null. – Immersive Jun 01 '18 at 06:05

0 Answers0