Reply To: Am I using classes correctly?

Forum Archive Am I using classes correctly? Reply To: Am I using classes correctly?

#38760
Jonathan Gonzalez
Participant

    The error you’re getting is actually not a class issue, but a local method issue. Whenever you declare a variable within a method it is only accessible to that method. If you added the “cannon.Update()” in the start method you’ll notice that error won’t appear. That said the way you’re approaching this is not ideal. 

    Instead of including various things within any of your update methods, create separate methods for the various game mechanics. You kind of already did this with your “ShootGun” method, do the same for the others.  I’m not sure about how your methods work, but I can organize them a bit. 

    So for everything within the update method create a separate method:

    public void CheckDelay () {
             bulletDelay--;
    
            if (bulletDelay < 0 && OVRInput.Get(OVRInput.Button.SecondaryIndexTrigger))
            {
                audioSource.Play();
                bulletDelay = fireSpeed;
                ShootGun();
            }
    }
    

    We make it public so that we can access it from other classes. Since this is being called in another script, you don’t need to constantly update it within the update method in another class. It’s being updated every time it is called in another class. 

    In your second class you could call it as such:

    void Update ()
    {
    cannon.CheckDelay();
    }