C# health decrease on collision

I’m still very new to scripting and have learned a lot in the last few weeks but I just cannot seem to get this to work for anything. I’ve searched the forums over and over and nothing seems to work after much trial and error oh god at the errors. So I’ve finally decided to bite the bullet and ask for help. Any clarification on this subject is much appreciated.

using UnityEngine;
using System.Collections;

public class health : MonoBehaviour {

public float hp = 100f;
public float gold = 0f;

void Start () {

}

void FixedUpdate () {
	//adds gold to buy items with
	gold += 1 * Time.smoothDeltaTime.CompareTo (-1) ;

}

	
	// learn how to customize and add buttons for spawning player sprites
void OnGUI(){
		GUI.Box (new Rect (0, 0, 100, 25), "HEALTH" + " = " + hp);

		GUI.Box (new Rect (0, 30, 100, 25), "GOLD" + " = " + gold);
	}

void OnCollisionEnter2D (Collider2D collision){

	if (collision.gameObject.name == "enemy")
					hp -= 1;

Since you did not write it I assume that your code should work so maybe your objects are not setup correctly and are not colliding at all. To prove my assumption add the following code to the line 27

print("We have collision");

Run the game and collide with the enemy, check it console spits the above mention message.
If not, your setup is probably wrong, check in this tableif your objects are correctly setup. Among others:

  1. Do you have colliders in both of the colliding gameObjects?
  2. Is “Is trigger” disabled in both of them?
  3. Are the colliders of 2D type (As in your code you check for 2d collision)?
  4. Does at least one of the colliders have a rigidbody2D component?

Check this stuff and maybe describe more in detail what are you trying to achieve and where exactly you reckon it fails.

BTW you can move your gold equations to normal void Update(), leave FixedUpdate() for a physics related stuff like moving chars etc.

Actually since it took so long for my question to get posted. I came across the answer a few hours after posting my question. It seems it always happens like that my new code looks like this. You recommend to put the gold in a normal update? could you elaborate a little bit. Thank you very much for posting and btw I forgot to answer you that all of my colliders are 2d and there is rigidbodies. and is trigger is off on all objects. Im pretty comfortable with using unity its just the coding part thats giving me difficulty there is soo much to learn for someone with absolutely no scripting experience. but i have always been book smart and a fast learner so im getting there just need to study study study.

I am working on a type of tower defense game. Multiplayer. The kind where there is a tower for each player on opposite ends of the screen and the goal is to destroy the other player with sprites that you can buy for gold. So as you can imagine I still have a lot of GUI to add for spawning sprites and a lot more coding to do.

I’ve taken your advice and moved my gold equation into void update. Are there any other tips you could offer on this script?

using UnityEngine;
using System.Collections;

public class health : MonoBehaviour {

	public float hpleft = 100f;
	public float hpright = 100f;
	public float gold = 0f;



	void Update () {
		//adds gold to buy items with
		gold += 1f * Time.deltaTime;
		}

	// learn how to customize and add buttons for spawning player sprites
	void OnGUI(){
		GUI.Box (new Rect (0, 0, 100, 25), "HEALTH" + " = " +  (Mathf.Round (hpleft)));
		GUI.Box (new Rect (Screen.width - 100, 0, 100, 25), "HEALTH" + " = " +  (Mathf.Round (hpright)));
		GUI.Box (new Rect (0, 30, 100, 25), "GOLD" + " = " + (Mathf.Round (gold)));
		}

	void OnCollisionStay2D(Collision2D coll) {
		if (coll.gameObject.tag == "enemy")
			hpleft -= 1 * Time.deltaTime ; 

	}
}