0
\$\begingroup\$

i want to get a component value from the game object which collides with my trigger's object script. this is my code on the trigger:

 void OnTriggerEnter (Collider obj){
     if(obj.GetComponent<motor>().enabled == true){
         passed = true;
     }
 }

and this is the error i get:

NullReferenceException: Object reference not set to an instance of an object
 cp.OnTriggerEnter (UnityEngine.Collider obj) (at Assets/Scripts/cp.cs:15)

also when changing the code on the trigger:

 motor mObj = obj.gameObject.GetComponent();
    if(mObj != null){
         //code here
         Debug.Log("mObj not null");
     }else{
             Debug.Log("mObj is null");
     }

it returns

mObj is null

but the parent obj does have that 'motor' script, other scripts in the car which enter the trigger can access it

\$\endgroup\$
4
  • \$\begingroup\$ <motor> is case sensitive? \$\endgroup\$ Aug 25, 2015 at 13:02
  • \$\begingroup\$ the script name is all lowercase @dnkdrone.vs.drones \$\endgroup\$ Aug 25, 2015 at 13:03
  • \$\begingroup\$ try "motor m = obj.GetComponent<motor>();" \$\endgroup\$ Aug 25, 2015 at 13:05
  • \$\begingroup\$ tred that: 'code' motor mObj = obj.gameObject.GetComponent(); if(mObj != null){ //code here Debug.Log("mObj not null"); }else{ Debug.Log("mObj is null"); } 'code' returned 'code' mObj is null 'code' \$\endgroup\$ Aug 25, 2015 at 13:09

3 Answers 3

3
\$\begingroup\$
void OnTriggerEnter (Collider obj){
     if(obj.GameObject.GetComponent<motor>().enabled == true){
         passed = true;
     }
 }

adding this GameObject --> obj.GameObject.GetComponent().enabled would just work :)

let me know if it works

\$\endgroup\$
0
\$\begingroup\$

Make the following changes to the code.

    motor mObj = obj.gameObject.GetComponent<motor>(); 
    if(mObj != null){
         //code here
         Debug.Log("mObj not null");
     }else{
             Debug.Log("mObj is null");
     }

On trigger will only work if the game object to which this script is attached have a collider. It won't work if only the children have colliders.

\$\endgroup\$
2
  • \$\begingroup\$ returns "mObj is null" \$\endgroup\$ Aug 25, 2015 at 13:12
  • \$\begingroup\$ the object to enter the trigger is a car with only a colder on its body (currently just a cube) and wheel collides, would the wheel collides be the issue? \$\endgroup\$ Aug 25, 2015 at 13:16
0
\$\begingroup\$

i found that it must be the wheel collides or some other child causing the issue anyway here is the code i now use

    void OnTriggerEnter (Collider obj){


    motor mObj = obj.transform.parent.gameObject.GetComponent<motor>();
    if(mObj != null){
        //code here
        Debug.Log("mObj not null");
        if(mObj.enabled == true){
            Debug.Log("cp passed");
        passed = true;
        }
    }else{
        Debug.Log("mObj is null");
    }

}

thank you for helping

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .