Why am i getting errors?
using UnityEngine;
using UnityEngine.Networking;
public class PlayerShoot : NetworkBehaviour {
private const string PLAYER_TAG = "Player";
public PlayerWeapon weapon;
[SerializeField]
private Camera cam;
[SerializeField]
private LayerMask mask;
void Start ()
{
if (cam == null)
{
Debug.LogError ("PlayerShoot: No camera referenced");
this.enabled = false;
}
}
void Update ()
{
if (Input.GetButtonDown ("Fire1"))
{
Shoot();
}
}
[Client]
void Shoot ()
{
RaycastHit _hit;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, weapon.range, mask) )
{
if (_hit.collider.tag == PLAYER_TAG)
{
CmdPlayerShot(_hit.collider.name, weapon.damage);
}
}
}
[Command]
void CmdPlayerShot (string _playerID, int _damage)
{
Debug.Log(_playerID + " has been shot.");
Player _player = GameManager.GetPlayer(_playerID);
_player.TakeDamage(_damage);
}
}
i am getting the errors The best overloaded method match for PlayerShoot.CmdPlayerShot(string, int)' has some invalid arguments Argument
#2' cannot convert float' expression to type
int'
please help me!
Answer by Landern · Sep 29, 2016 at 02:17 PM
On line:42 apparently weapon.damage is of float type but your method CmdPlayerShot expects a type int for the second parameter named _damage.
You can either cast to int with a small change:
CmdPlayerShot(_hit.collider.name, (int)weapon.damage);
Or you can update your methods to take float instead of int including TakeDamage method of the Player class.
Your choice, the former is quicker from an implementation standpoint.
Your answer
![](https://koobas.hobune.stream/wayback/20220612093650im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can I replace objects in the initial position? 0 Answers
Unity won't build my scripts 0 Answers
Mobile attack button isn't doing damage to enemy 0 Answers
How could i save a variable called by multiple objects using the same script? 1 Answer
how to manage different objects with the same script. 0 Answers