Combat Script - how to take damage

So, my problem is not so simple. I asked a lot of people that are good with Unity but i didn’t get answer.

I started to make a simple combat game with cubes and there is (for now) 4 player (or 4 cubes). Every cube have similar name - Blue Cube, Red Cube, Black Cube and White Cube. Every cube have own script for moving, firing, taking damage etc. with same name as player name (Blue Player etc.) and every player have their own bullet script for dealing damage ( i used SendMessage for dealing damage) and now there is a problem - I need more parameters in TakeDamage function so i can know killer. Biggest problem here is that i don’t know how i can make it without scripts for each player.

Here is my script for Blue Player :

using UnityEngine;
using System.Collections;

public class RedPlayer : MonoBehaviour {
	//Vars
	private BasePlayer redPlayer = new BasePlayer();
	public bool isAlive;
	public Rigidbody bullet;
	public GameObject weapon;

	void Start() {
		redPlayer.Name = "RedPlayer";
		redPlayer.MaxHp = 200;
		redPlayer.CurrHp = redPlayer.MaxHp;
		redPlayer.FireDamage = 15;
		redPlayer.FireCooldown = 2f;
		redPlayer.MoveSpeed = 2.5f;
		redPlayer.RotateSpeed = 70;
		isAlive = true;
	}

	void Update () {
		
		if(false){
			//Moving and rotating player
			transform.Translate(0,0, redPlayer.MoveSpeed * Input.GetAxis("Vertical") * Time.deltaTime);
			transform.Rotate(0, redPlayer.RotateSpeed * Input.GetAxis("Horizontal") * Time.deltaTime,0);
			
			//Firing
			if(Input.GetKeyDown("space")) {
				Rigidbody clone = Instantiate(bullet, weapon.transform.position, transform.rotation) as Rigidbody;
				clone.velocity = transform.TransformDirection(new Vector3(0,0,60));
			}
		}

	}

	//Take Damage Function
	public void TakeDamage(float damage, string killer){
		redPlayer.CurrHp -= damage;
		Debug.Log("Blue player has taken " + damage + " damage and now have " + redPlayer.CurrHp + " hp.");

		//Checking player hp
		if(redPlayer.CurrHp <= 0) {
			isAlive = false;
			Die (killer);
		}
	}

	void Die(string killer) {
		gameObject.SetActive(false);
		print(killer);
	}
}

And script for Blue bullet :

using UnityEngine;
using System.Collections;

public class BlueBullet : MonoBehaviour {

	public float damage = 7;
	
	void OnTriggerEnter(Collider coll) {
		print(coll.gameObject.name);

		if(coll.gameObject.tag == "Player") {
			coll.gameObject.SendMessage("TakeDamage",damage, SendMessageOptions.RequireReceiver);
		}

		Destroy(gameObject);
	}
}

If Anyone knows better way, i would be very happy to hear it.

Hmmm, in my opinion you are going about this all wrong. Did you just copy paste the code and create a new class for every player? Instead, just have 1 class, and some strings and the variables for each players. Then it’s easy to just call the method as you would normally.

coll.gameObject.GetComponent<TEHPLAYA>().takeDamage(as, many, things, as, I, wish);

:slight_smile: