Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

GetComponentInParent not working

Discussion in 'Scripting' started by Jason_Hipkins, Aug 29, 2016.

  1. Jason_Hipkins

    Jason_Hipkins

    Joined:
    Dec 23, 2015
    Posts:
    57
    Hello,
    I'm trying to set up a radius that causes a parent collider to turn off and on, for a teleporter or something that you have to take a step back before walking back up to it to turn it back on again. For some reason, my code:

    voidOnTriggerExit(Collidertarget)
    {
    if(target.gameObject.tag.Equals("Player")==true){
    GetComponentInParent<Collider>().enabled=true;

    }
    }
    Isn't re-activating the parent collider. It is getting hit because debug.logs can come up in the debugger, but the actual collider never wakes back up. Is my syntax wrong? Thanks
     
    Ealbinu likes this.
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    First I'd like to say you can modify your if statement to be better.

    if(target.gameObject.tag == "Player") looks much better.

    Next, for a possible attempt to fix your problem. I'm assuming you have a parent collider and a child with a collider as well? The child is triggering the parent collider to turn off and on? A possible check is to assign the collider to a variable when you enter and see if that works. I'm assuming that your OnTriggerEnter is working since it sounds like it's being turned off.

    Code (CSharp):
    1. private Collider parentCollider;
    2.  
    3. void OnTriggerEnter(Collider other)
    4. {
    5.    if(other.gameObject.tag == "Player")
    6.    {
    7.        parentCollider = GetComponentInParent<Collider>();
    8.       parentCollider.enabled = false;
    9.    }
    10. }
    11.  
    12. void OnTriggerExit(Collider other)
    13. {
    14.    if(other.gameObject.tag == "Player")
    15.    {
    16.       parentCollider.enabled = true;
    17.    }
    18. }
    You can try something like this to see if it's working correctly. Add some debugs within the if statements as well. And if nothing else, do a print on parentCollider.gameObject to make sure it is targetting what it is suppose to(you could do this print/debug.log now as well to double check).

    Actually, I'm looking at http://docs.unity3d.com/ScriptReference/Component.GetComponentInParent.html and it seems the GetComponentInParent returns the component on the gameObject you hit as well. So it's a good chance you are getting the child collider.

    You might be better off doing

    Code (CSharp):
    1. transform.parent.getComponent<Collider>().enabled = true;
     
    MGGDev likes this.
  3. GrischaG

    GrischaG

    Joined:
    Apr 26, 2013
    Posts:
    40
    Hi Jason,
    is it possible, that your collider needs to be enabled to trigger your function, but in this situation he is disabled?

    Grischa
     
  4. Jason_Hipkins

    Jason_Hipkins

    Joined:
    Dec 23, 2015
    Posts:
    57
    Hello, thanks for the responses. Found it was finding the collider on itself and stopping. When I changed the .enabled to false I saw that it wasn't going up the chain to the parent, rather just turning itself off.
    Instead now I have it going up a layer to a script on the parent object (which is running anyway) to turn the collider back on
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,139
    Yeah, I modified my post after looking at getComponentInParent. Added code that should fix it.