1
\$\begingroup\$

I am looking to decrease the value stored in my script "progress" which takes 1 away from a value curHealth, when the Player sprite to which this script "collision", is attacked/ touching the Enemy 2d box collider. The decreasing of the value doesn't happen when the two objects touch. Both the Player and the Enemy have 2dbox colliders and other shape 2d colliders.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class collision : MonoBehaviour {

    public progress Progress = GameObject.FindGameObjectWithTag("HealthBar").GetComponent<progress>();



    void OnCollisionStay2d(Collision2D coll){
        if (coll.gameObject.name == "Enemy") {

            Progress.curHealth -= 1;

            }
        }

}
\$\endgroup\$
  • \$\begingroup\$ can you see on the editor that "public progress Progress" is assigned correctly? \$\endgroup\$ – Thalthanas Jul 14 '17 at 7:12
  • \$\begingroup\$ Yes it was, just as @Tolga Sen pointed out, I capitalized the object and not the class. \$\endgroup\$ – Mike noah Jul 14 '17 at 20:29
1
\$\begingroup\$

Your OnCollisionStay2D function has a typo. Capitalize the letter "d" at the end, then it will work if there is not another problem. And also another tip, please do capitalize the first letter of Classes, not the objects.

\$\endgroup\$
  • \$\begingroup\$ Thank you so much! Worked right away. I will, thank you! \$\endgroup\$ – Mike noah Jul 14 '17 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.