Duplicated Weapon script not working

Hello everyone,

I have a 2D platform game with a player and enemies. I have a small problem duplicating a WeaponScript for the player. I need to duplicate it because player and enemies are using the same script, but I want diffrent adjustment for each.

First, I attach the script to the player (as seen bellow). Then, I duplicate the same script, call it WeaponScript2, change the global name and drag and drop the new script2 to the player. The player suddenly does not shoot.

Where is the problem?
The same thing occurs when I duplicate the HealtScript and so on.

using UnityEngine;

/// <summary>
/// Launch projectile
/// </summary>
public class WeaponScript2 : MonoBehaviour
{
	//--------------------------------
	// 1 - Designer variables
	//--------------------------------
	
	/// <summary>
	/// Projectile prefab for shooting
	/// </summary>
	public Transform shotPrefab;
	
	/// <summary>
	/// Cooldown in seconds between two shots
	/// </summary>
	public float shootingRate = 0.25f;

	//--------------------------------
	// 2 - Cooldown
	//--------------------------------
	
	private float shootCooldown;
	
	void Start()
	{
		shootCooldown = 0f;
	}
	
	void Update()
	{
		if (shootCooldown > 0)
		{
			shootCooldown -= Time.deltaTime;
		}
	}
	
	//--------------------------------
	// 3 - Shooting from another script
	//--------------------------------
	
	/// <summary>
	/// Create a new projectile if possible
	/// </summary>
	public void Attack(bool isEnemy)
	{
		if (CanAttack)
		{
			shootCooldown = shootingRate;
			
			// Create a new shot
			var shotTransform = Instantiate(shotPrefab) as Transform;
			
			// Assign position
			shotTransform.position = transform.position;
			
			// The is enemy property
			ShotScript shot = shotTransform.gameObject.GetComponent<ShotScript>();
			if (shot != null)
			{
				shot.isEnemyShot = isEnemy;
			}
			
			// Make the weapon shot always towards it
			MoveScript move = shotTransform.gameObject.GetComponent<MoveScript>();
			if (move != null)
			{
				move.direction = this.transform.right; // towards in 2D space is the right of the sprite
			}
		}
	}
	
	/// <summary>
	/// Is the weapon ready to create a new projectile?
	/// </summary>
	public bool CanAttack
	{
		get
		{
			return shootCooldown <= 0f;
		}
	}
}

You don’t need to duplicate the script at all. Scripts are fine being attached to different GameObjects.

To reference that script:

private WeaponScript playerWepScript;
private WeaponScript enemyWepScript;

void Start()
{
    GameObject pc = FindGameObjectWithTag("Player");
    playerWepScript = pc.GetComponent<WeaponScript>();

    //or all in one go like this
    enemyWepScript = FindGameObjectWithTag("Enemy").GetComponent<WeaponScript>();
}

Of course if you have lots of enemies then you might want to have a way to tell one from another so when you instantiate them add the script and edit an int so you know which one’s which.

instance = (GameObject)GameObject.Instantiate(EnemyPrefab);

instance.AddComponent<WeaponScript>().MyCount = cnt;

cnt = cnt + 1;

That way you can check which object you’re referencing.

You’ll need to put a public int MyCount; in your script.

Hope that makes sense.