Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

My fire1 code does not work

Discussion in 'Scripting' started by MrDragosia, Jun 27, 2022.

  1. MrDragosia

    MrDragosia

    Joined:
    May 28, 2021
    Posts:
    3
    I have tryed to fix this for long time but it does not work and i don't get any errors, if you need the hole script just tell me.


    void Update ()
    {
    if (Input.GetButtonDown("Fire1"))
    {
    Shoot();
    }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    This could mean absolutely anything. The "code" here is so minimal it doesn't show anything either. You need to provide some information if you want help.

    So you've been debugging it right? What were you expecting to happen, what does/doesn't happen and what were the results of debugging? The basics you should've figured out are things like, is the "Update" method being called, is the "Shoot" method being called etc.
     
  3. MrDragosia

    MrDragosia

    Joined:
    May 28, 2021
    Posts:
    3
    here is the hole code


    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.RpcTakeDamage(_damage);
    }

    }
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Posting the whole code doesn't take any effort and doesn't describe what "doesn't work" and what debugging you've done or not done. Also, please use code-tags.

    This above is essentially known as a lazy post, sorry to say.
     
  5. MrDragosia

    MrDragosia

    Joined:
    May 28, 2021
    Posts:
    3
    i fixed it, forgot a tag on the player so there was no problem with the code