5
\$\begingroup\$

I realise other posts exist with this topic yet none have gone into enough detail for me. I am attempting to create a 2D game in Unity using C# as my scripting language.

Basically I have two objects, player and bomb. Both were created simply by dragging the respective PNG to the stage.

I have set up touch controls to move player left and right; gravity of any kind is not needed as I only require it to move x units when I tap either the left or right side of the screen. This movement is stored in a script called playerController.cs and works just fine.

I also have a variable health = 3 for player, which is stored in healthScript.cs.

I am now at a point where I am stuck. I would like it so that when player collides with bomb, health decreases by one and the bomb object is destroyed. So what I tried doing is using a new script called playerPhysics.cs, I added the following:

void OnCollisionEnter2D(Collision2D coll){
        if(coll.gameObject.name=="bomb")
        {
            GameObject.Destroy("bomb");
            healthScript.health -= 1;
        }
}

While I'm fairly sure I don't know the proper way to reference a variable in another script and that's why the health didn't decrease when I collided, bomb never disappeared from the stage so I'm thinking there's also a problem with my collision.

Initially, I had simply attached playerPhysics.cs to player. After searching around though, it appeared as though player also needed a rigidBody attached to it, so I did that. Still no luck. I tried using a circleCollider (player is a circle), using a rigidBody2D, and using all manner of colliders on one and/or both of the objects. If you could please explain what colliders (if any) should be attached to which objects and whether I need to change my script(s), that would be much more helpful than pointing me to one of the generic documentation examples I've already read. Also, if it would be simple to fix the health thing not working that would be an added bonus but not exactly the focus of this question.

Bear in mind that this game is 2D; I'm not sure if that changes anything.

Thanks!

\$\endgroup\$
2
  • 4
    \$\begingroup\$ You should add braces to include both lines in your branch. Also, to destroy the other object you should type Destroy(coll.gameobject) \$\endgroup\$
    – OMGtechy
    Aug 19, 2014 at 1:58
  • \$\begingroup\$ I also had really a lot of problems when configuring the collision between game object, after years I wrote every tips in this article gamedevelopertips.com/unity-collision-detection-2d, I'm sure it'll be helpful if you want to set up properly you collider/project \$\endgroup\$ May 24, 2020 at 15:48

1 Answer 1

11
\$\begingroup\$

I did the following to achieve results:

Added a Rigidbody2D to the player (deactivate Gravity). Added a 2D-Collider to the bomb. Added control and health script (basic stuff, just position updates). My health is just a public variable. Used the following code in playerPhysics.cs:

void OnCollisionEnter2D(Collision2D coll)
{
    if (coll.gameObject.name == "bomb")
    {
        Destroy(coll.gameObject);
        this.GetComponent<healthScript>().health -= 1;
    }
}

Note the way I fetched the HealthScript. If you do this on a regular basis, you may want to store this in a private Variable of type healthScript.

private healthScript hs = this.GetComponent<healthScript>();

//...usage in methods
hs.health = 9001;
\$\endgroup\$
3
  • \$\begingroup\$ Good use of caching Components, vital in Unity when using the same value again and again. \$\endgroup\$ Aug 19, 2014 at 13:51
  • \$\begingroup\$ Thank you so much! Aside from the braces problem (for some reason the Unity docs don't use braces in their examples), it was that BOTH the object colliding AND the object being collided with need to have Colliders, PLUS at least one needs to have a RigidBody. All I've ever seen is people saying to add a Collider and a RigidBody. Your solution worked beautifully however. \$\endgroup\$
    – Matt
    Aug 20, 2014 at 3:18
  • 1
    \$\begingroup\$ @N1ghtshade3 if only one has a collider, you would try to collide something that occupies space with something that occupies no space at all. Also, to increase performance, attach the rigidBody to the player, as there is at most one player, but possibly infinite bombs (I assume). Generally you want to create as less as possible of anything, regardless if it is a complicated function call, a rigidBody or something else. For every component you add to your gameobjects, ask yourself "Will this also work without the component? ". On another note: Will you publish it and if yes were? I love to test \$\endgroup\$
    – JFBM
    Aug 20, 2014 at 11:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .