- Home /
 
              This question was 
             closed Dec 16, 2014 at 12:14 PM by 
             thef1chesser for the following reason: 
             
 
            The question is answered, right answer was accepted
 
               Question by 
               thef1chesser · Dec 16, 2014 at 12:01 PM · 
                c#nullreferenceexceptionnullif statement  
              
 
              Checking if an object is null, results in NRE
     void OnTriggerEnter(Collider col)
     {
         if (m.Target == null)
         {
             if (col.gameObject.tag == "Player")
             {
                 Debug.Log("found player");
                 m.Target = col.transform;
             }
         }
     }
 
               So I have this code where I want to set a player as a target as long as there is no target. Target is a Transform.
The problem I have here is that it gives me NullReferenceExceptions on the check.
So what do you guys think as a possible solution. How to do this?
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by gjf · Dec 16, 2014 at 12:04 PM
you need to be checking m for null too
well m was null indeed. I need to set m better in this case.