0

I am trying to get the tag of the parent of a collider:

  _____Hierarchy_____
 | EmptyGameObject   | <-- Has the tag "Generator"
 |   ColliderObject  | <-- Gets hit by Raycast (Has the collider)
 |___________________|

But i get a error. I have no idea how to fix it.

The error is at line: 1

else if (hit.collider.transform.parent.gameObject.tag == "Generator" && Input.GetKeyDown ("e"))

Script:

else if (hit.collider.transform.parent.gameObject.tag == "Generator" && Input.GetKeyDown ("e"))
{
    if (Fuel >= 100)
    {
        Fuel -= 100;
        Generator.Machine_Fuel += 100;
        ES2.Save(Fuel,  "Data/Player/Inventory/Fuel.txt");
        Debug.Log ("100ml Fuel added to machine");
        // txtRef.text = "Apples: " + apples;
    }
    else
    {
        Generator.Machine_Fuel += Fuel;
        Fuel -= Fuel;
    }
}

Thanks!

-shinevision

shinevision
  • 3
  • 1
  • 5
  • 1
    Why don't you debug your code, put a breakpoint on the line in question and check which of the references used on that line is null? – Eduard Malakhov Jan 23 '17 at 15:33
  • I used your edit. Thanks! – shinevision Jan 23 '17 at 15:46
  • Cracks me up when people mark as a duplicate before going into the problem. @Shievision - what do you see when you Debug.Log(hit.collider.transform.parent.gameObject)? Put this before the line that errors. – Absinthe Jan 23 '17 at 19:56
  • @Absinthe It works. but when i look at the ground (Terrain) i get the same error... weird. it doesnt error if i point at differend objects. only the terrain. maybe im missing a check that checks if it even has a parent maybe? (Im kinda new to programming so im sorry) – shinevision Jan 24 '17 at 00:10
  • @Absinthe with "it worked" i mean that it displays the correct object. sorry for my bad english – shinevision Jan 24 '17 at 00:12
  • Post a new question on http://gamedev.stackexchange.com/ describing exactly what you're trying to do and I'll take a look – Absinthe Jan 24 '17 at 08:42

0 Answers0