Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Door not opening on collision

Discussion in 'Scripting' started by denissuu, Dec 11, 2019.

  1. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    162
    Hi there! I have been trying to make it so when my enemy hits a door, the door executes my dooropen script and makes it open.If I press the use button on my player on the door they open, so the script is good pretty much, but I don't know how to make it so when the enemy walks into a door that door open script executes.
    Here is the code I tried:

    Code (CSharp):
    1.  //Forgot to copy the namespaces and stuff but they were there anyways      
    2. public GameObject Enemy;
    3.  
    4. public void OnCollisionEnter(Collision collision) {
    5. if (collision.gameobject.CompareTag("Enemy"))
    6.         {
    7.             GetComponent<dooropen>().ChangeDoorState();
    8.         }
    9. }
    10.  
    My enemy had his tag put on there also.I have tried also using triggers and stuff, I just can't get it to work , it's been around 2 hours already.Does anyone know how I could do it properly?
    I sometimes was getting an error about the comparetag while using some other variants of code something like " the comparetag (or other words, depends) is not (...) Are you missing an assembly reference or a Directive? "
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    Does your enemy or door have a rigidbody attached?
     
  3. denissuu

    denissuu

    Joined:
    Nov 6, 2019
    Posts:
    162

    My enemy only has a collider.
    And neither the door, I'm not really sure, I will check when I'm home.
    What about it though?
    How could that affect things?
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186