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 How do I fix these errors?

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

  1. TrexMatrix

    TrexMatrix

    Joined:
    Aug 8, 2023
    Posts:
    39
    This is my second day of coding and I am having trouble figuring out this errors can someone please help me?

    These are the errors:
    • Assets\Scripts\Gun.cs(23,67): error CS1061: 'Transform' does not contain a definition for 'foward' and no accessible extension method 'foward' accepting a first argument of type 'Transform' could be found (are you missing a using directive or an assembly reference?)
    • Assets\Scripts\Gun.cs(23,79): error CS0246: The type or namespace name 'Raycast' could not be found (are you missing a using directive or an assembly reference?)
    • Assets\Scripts\Gun.cs(41,19): error CS0246: The type or namespace name 'NotImplementedException' could not be found (are you missing a using directive or an assembly reference?)
    This is my script:
    using UnityEngine;

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

    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(transform.position, transform.foward, out Raycast hitInfo, gunData.maxDistance))
    {
    Debug.Log(hitInfo.transform.name);
    }

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

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

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

    }

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

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    ijmmai likes this.
  3. TrexMatrix

    TrexMatrix

    Joined:
    Aug 8, 2023
    Posts:
    39
    i dont usually post i just need help with these errors, ive fixed many before this im just totally brain dead on these
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,769
    Well the first one is just a typo: https://docs.unity3d.com/ScriptReference/Transform-forward.html

    The second is you're using Raycast, when it should be RaycastHit: https://docs.unity3d.com/ScriptReference/RaycastHit.html

    The docs would've told you that: https://docs.unity3d.com/ScriptReference/Physics.Raycast.html

    The third one is because all of C# standard exceptions are in the System namespace, same reason as your last thread. Again, you can just google this stuff if you don't know what namespace they belong to. Though I have no idea why you bother with exceptions like these while you're learning.
     
    Yoreki likes this.
  5. TrexMatrix

    TrexMatrix

    Joined:
    Aug 8, 2023
    Posts:
    39
    thanks dude, ill figure them out on my own from now on, I've had some coding experience in the past but that was with lua, its been 2 days of c# so im still getting used to it. Thanks tho!