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

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.
1974913--128013--code.png

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

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.

I’ve fixed the typo thing before I uploaded it here :smile: 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

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.

Yes I was working on that right after I post this, this is my code for them now, still doesn’t work

MEDICBOX:

public class MedicBoxInScene : MonoBehaviour {

  public string thingToAdd = "currentValue";
  public int amountToAdd = 100;

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
   

    }

  void OnTriggerEnter(Collider other)
  {
    Debug.Log("I'm triggerd");

    if (other.gameObject.tag == "Player")
    {
      GameObject.Find("Player").GetComponent<myInventory>().CreateMedicBox(thingToAdd, amountToAdd);
      Destroy(gameObject);
    }
   
  }
}

WATERBOTTLE:

public class WaterBottleInScene : MonoBehaviour {

  public string thingToAdd = "currentValue";
  public int amountToAdd = 20;
  public int doubleIt = 2;

    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {
   
    }

  void OnTriggerEnter(Collider other)
  {
    if (other.gameObject.tag == "Player")
    {
      GameObject.Find("Player").GetComponent<myInventory>().CreateWaterBottle(doubleIt, thingToAdd, amountToAdd);
      Destroy(gameObject);
    }

  }
}

Waterbottle works, Medicbox doesn’t… really really weird

can you upload a test project? im curious now

http://we.tl/fVp8Mnphou
The other code is not cleaned up yet :smile:

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

hope this works :smile:

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…

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

for the bulletholes, otherwise the bulletholes would be floating on the collider… wich looks pretty odd :smile:

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.

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.

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?

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

fixed it by just re-importing the asset package