Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

OnTriggerEnter stopped working for 2/3 while code of the 3 objects are the same

Discussion in 'Physics' started by Brentjebeest852, Feb 22, 2015.

  1. Brentjebeest852

    Brentjebeest852

    Joined:
    Feb 20, 2015
    Posts:
    12
    Hi

    I am making an fps game as a school assignment, and I made an ammobox, medicbox and waterbottle, that are objects that can be picked up, you can probably already guess what they do.

    Medicbox: dissapears after picking up and gives health
    Ammobox: dissapears after picking up and adds ammo
    Waterbottle: dissapears after picking up and gives health + speed.

    The problem is, I created medicbox and ammobox, everything worked fine, then I created the water bottle and suddenly I can't pick up the medicbox/ammobox anymore. The triggering function oes not work for these 2 (when they did before ) I did not change any coding for them. They have the same code as the waterbottle, but the waterbottle CAN be picked up...

    Can you guys tell me what I did wrong please?

    note: I've updated other.gameObject.name == "firstAid" into "FirstAid" , so it matches the name, that did not fix it.
    code.png ammobox.png medicbox.png waterbottle.png
     
  2. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    you've got some typos in the firstaid thing. You've named the mesh "FirstAdd" and in your script, you're checking for "firstAid" (lowercase f) whereas it has an uppercase on the object.

    I don't know if either of these will solve your problem, but go fix them anyway, and then test again
     
  3. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    does your first debug message trigger when you walk over any box? (debug.log(other.gameobject.name))

    Also in future, be sure to post your code by inserting a code box, rather than a picture of it.
     
  4. Brentjebeest852

    Brentjebeest852

    Joined:
    Feb 20, 2015
    Posts:
    12
    I've fixed the typo thing before I uploaded it here :D but didn't work ...
    No it doesn't log the message... that's the weird thing, it just doesn't trigger...

    Ok next time i'll use a code box
     
  5. CodeMonke234

    CodeMonke234

    Joined:
    Oct 13, 2010
    Posts:
    181
    Best to create 3 scripts, one for each type of object.

    Each script implements ontriggerenter
    Each script will need reference to player.
    Make sure collider on object and trigger checkbox set.

    Use debug.log to verify collisions.
     
  6. Brentjebeest852

    Brentjebeest852

    Joined:
    Feb 20, 2015
    Posts:
    12
    Yes I was working on that right after I post this, this is my code for them now, still doesn't work

    MEDICBOX:

    Code (CSharp):
    1. public class MedicBoxInScene : MonoBehaviour {
    2.  
    3.   public string thingToAdd = "currentValue";
    4.   public int amountToAdd = 100;
    5.  
    6.     // Use this for initialization
    7.     void Start () {
    8.    
    9.     }
    10.    
    11.     // Update is called once per frame
    12.     void Update () {
    13.    
    14.  
    15.     }
    16.  
    17.   void OnTriggerEnter(Collider other)
    18.   {
    19.     Debug.Log("I'm triggerd");
    20.  
    21.     if (other.gameObject.tag == "Player")
    22.     {
    23.       GameObject.Find("Player").GetComponent<myInventory>().CreateMedicBox(thingToAdd, amountToAdd);
    24.       Destroy(gameObject);
    25.     }
    26.    
    27.   }
    28. }
    WATERBOTTLE:

    Code (CSharp):
    1. public class WaterBottleInScene : MonoBehaviour {
    2.  
    3.   public string thingToAdd = "currentValue";
    4.   public int amountToAdd = 20;
    5.   public int doubleIt = 2;
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.    
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.    
    15.     }
    16.  
    17.   void OnTriggerEnter(Collider other)
    18.   {
    19.     if (other.gameObject.tag == "Player")
    20.     {
    21.       GameObject.Find("Player").GetComponent<myInventory>().CreateWaterBottle(doubleIt, thingToAdd, amountToAdd);
    22.       Destroy(gameObject);
    23.     }
    24.  
    25.   }
    26. }
     
  7. Brentjebeest852

    Brentjebeest852

    Joined:
    Feb 20, 2015
    Posts:
    12
    Waterbottle works, Medicbox doesn't.. really really weird
     
  8. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    can you upload a test project? im curious now
     
  9. Brentjebeest852

    Brentjebeest852

    Joined:
    Feb 20, 2015
    Posts:
    12
    http://we.tl/fVp8Mnphou
    The other code is not cleaned up yet :D
     
  10. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    i can't seem to do much with this. There seems to be no scenes in the project, and there are compile errors in the code.

    The usual way to do this is to go to Assets > Export Package, and select everything. That will zip it up into a unitypackage file x

    can you give it another try? x
     
  11. Brentjebeest852

    Brentjebeest852

    Joined:
    Feb 20, 2015
    Posts:
    12
  12. Brentjebeest852

    Brentjebeest852

    Joined:
    Feb 20, 2015
    Posts:
    12
    Just added my script, the medicbox script, to another object, (baseballbat) and he picks that up... and runs the script, so I'm thinking the problem lies in the object, but how can an object be broken... lol..
     
  13. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    lots of ways, too many to list. I'll inspect your project and see what i find.

    Although right now, my main advice would be, why are you using mesh colliders for pickups? change those into sphere colliders with a reasonable radius
     
  14. Brentjebeest852

    Brentjebeest852

    Joined:
    Feb 20, 2015
    Posts:
    12
    for the bulletholes, otherwise the bulletholes would be floating on the collider.. wich looks pretty odd :D
     
  15. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    doyou really need to be able to shoot bulletholes in a medkit and water bottle?

    even if so, use two colliders, a mesh one inside a larger sphere. Use layers and collision masking to make your bullets ignore the sphere.
     
  16. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    i'm looking at your scene now. How exactly have you been testing things? there doesnt seem to be any kind of input or camera system implemented, play mode is basically functionless. I'll try dropping objects on the character anyway.
     
  17. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    Well i found what's wrong. Pretty much everything lol. Are you sure this is the project you've been testing with?

    The most notable problems
    -The player has no collider
    -the player isn't tagged player (or any tag)
    -the waterbottle, medkit and ammobox have no scripts on them.

    I scripted them all with a debug message in onTriggerEnter, stuck a capsule collider on your player, and moved them through him. they all trigger just fine.


    I have no idea what you've been doing, it looks like you've just deleted all the work and given me bare assets.

    What have you given me here?
     
  18. Brentjebeest852

    Brentjebeest852

    Joined:
    Feb 20, 2015
    Posts:
    12
    Uhm I don't know what you got, but when I send my zipped files to my group partner, he can just open it and play the same way I did... I think there must've gone something wrong... really weird, my partner can always open the stuff I send him
     
  19. Brentjebeest852

    Brentjebeest852

    Joined:
    Feb 20, 2015
    Posts:
    12
    fixed it by just re-importing the asset package