Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The name '' does not exist in the current context – Unity 3D

i get the errors The name ‘AK74’ does not exist in the current context and The name ‘STG44’ does not exist in the current context in both Debug.Log lines

any solution?

     private void Start()
    {
        weapon = GetComponent<Weapon>();

        Weapon AK74 = new Weapon(2, 30, 200);
        Weapon STG44 = new Weapon(1, 35, 200);

        _currentAmmoInClip = clipSize;
        STG44.Setmagsize(_currentAmmoInClip);

        _ammoInReserve = reservedAmmoCapacity;
        STG44.Setfullmag(_ammoInReserve);
        _canShoot = true;


    }

    private void Update()
    {
        Debug.Log(AK74.Getmagsize());
        Debug.Log(STG44.Getmagsize());
    }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Those variables are defined in the scope of Start method, and they will be deleted as soon as this function will finish. You want to store them in the object itself, so you have to declare them outside of function, in the class itself, like so:

Weapon AK74, STG44;

private void Start()
{
    weapon = GetComponent<Weapon>();

    AK74 = new Weapon(2, 30, 200);
    STG44 = new Weapon(1, 35, 200);

    _currentAmmoInClip = clipSize;
    STG44.Setmagsize(_currentAmmoInClip);

    _ammoInReserve = reservedAmmoCapacity;
    STG44.Setfullmag(_ammoInReserve);
    _canShoot = true;
}

private void Update()
{
    Debug.Log(AK74.Getmagsize());
    Debug.Log(STG44.Getmagsize());
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading