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. Dismiss Notice

Question What is this errors and how can it be fixed?

Discussion in 'Scripting' started by TrexMatrix, Aug 8, 2023.

  1. TrexMatrix

    TrexMatrix

    Joined:
    Aug 8, 2023
    Posts:
    39
    When I finished coding my script I came across this error that I don't know how to fix can someone please help me?

    These are the errors:
    Assets\Scripts\Gun.cs(25,37): error CS0103: The name 'muzzle' does not exist in the current context
    Assets\Scripts\Gun.cs(41,23): error CS0103: The name 'muzzle' does not exist in the current context
    Assets\Scripts\Gun.cs(41,40): error CS0103: The name 'muzzle' does not exist in the current context

    This is my script:

    using UnityEngine;
    using System;

    public class Gun : MonoBehaviour
    {
    [Header("References")]
    [SerializeField] private GunData gunData;
    [SerializeField] private Transform Muzzle;

    float timeSinceLastShot;

    private void Start()
    {
    PlayerShoot.shootInput += Shoot;
    }

    private bool CanShoot() => !gunData.reloading && timeSinceLastShot > 1f / (gunData.fireRate / 60f);

    public void Shoot()
    {
    if (gunData.currentAmmo > 0)
    {
    if (CanShoot())
    {
    if (Physics.Raycast(muzzle.position, transform.forward, out RaycastHit hitInfo, gunData.maxDistance))
    {
    Debug.Log(hitInfo.transform.name);
    }

    gunData.currentAmmo--;
    timeSinceLastShot = 0;
    OnGunShot();
    }
    }
    }

    private void Update()
    {
    timeSinceLastShot += Time.deltaTime;

    Debug.DrawRay(muzzle.position, muzzle.forward);
    }

    private void OnGunShot() {
    throw new NotImplementedException();
    }

    }

    Can someone please help me figure this out?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,019
    Muzzle and muzzle are two different things. Case matters! And so does the use of code tags when posting code ...
     
    bugfinders likes this.
  3. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,124
    C# is case sensitive. You've capitalized the definition but aren't using it that way.
     
  4. TrexMatrix

    TrexMatrix

    Joined:
    Aug 8, 2023
    Posts:
    39
    Thanks I can't believe I didn't think of that!
     
  5. Chubzdoomer

    Chubzdoomer

    Joined:
    Sep 27, 2014
    Posts:
    106