#Question about events

1 messages · Page 1 of 1 (latest)

edgy jay
#

Hello, I am a new Unity programmer and I'm just starting to use my first events. I have a controller script, that plays some animations using coroutines. I have two sliders on the scene and a slider script attached to both. It is my understanding these two scripts are completely seperate instances. Both sliders have "On value changed" function assigned, using different functions on the class...sliderOne(), sliderTwo(). These methods call the events like this:

public class SliderScript : MonoBehaviour
{
    [SerializeField] public ControllerScript controller;
    public static event Action ActionCompleteOne;
    public static event Action ActionCompleteTwo;

    // Update is called once per frame
    public void ChangeOne(float value)
    {
        if (value > 0f && value <= .05f){
        if (TunerComplete != null) // new
            ActionCompleteOne();
        }
    }
}

I am now trying to use the controller to add events to ActionCompleteOne. What I'm not understanding is variable scope for these class objects. I followed a bunch of guides and they simply have me assign it as if the class was available. If this is correct, I don't get how it is available...

public class ControllerScript : MonoBehaviour
{
    public ExampleScript exampleController;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        SliderScript.ActionCompleteOne += AnimationContinue;
        StartCoroutine(AnimationStart());
    }

    private IEnumerator AnimationStart()
    {
        // animation runs
    }
    
    private void AnimationContinue()
    {
    
    }
}
#

I am currently attaching Controller to my slider so it can call exampleController. I thought that was the right approach but this make it seem like a waste of time. Can I just call Controller.exampleController.exampleMethod() anytime I want. Why do I have to attach it? Same goes for the event sliders...do have a way to attach a global class to them so I don't have to mess around with that as well? Sorry for the scattered thoughts but I'm a bit lost.

azure pulsar
# edgy jay Hello, I am a new Unity programmer and I'm just starting to use my first events....

What I'm not understanding is variable scope for these class objects
It's a public static event, public meaning anything can access it. Static means it's associated with the type SliderScript rather than an instance of SliderScript. You really dont need a static event here.
The controller script could just have a reference to the SliderScript and subscribe to the event on that instance.

coarse folio
#

static is the key problem here

azure pulsar
#

its the same outcome if you subscribe to the event compared to manually calling every method, but the design changes heavily. Like your SliderScript relies on a controller existing with the exampleController setup

edgy jay
#

Ok so the normal appraoach would be to add the following line to controller

public SliderScript sliderController

and then assign to the event like I had in the example

#
SliderScript.ActionCompleteOne += AnimationContinue;
azure pulsar
#

SliderScript is still the Type, the instance is sliderController

edgy jay
#

and it sounds like static methods are available everywhere

azure pulsar
#

its available everywhere because its public

edgy jay
#

@azure pulsar : I'm still learning the proper words...sliderController is the class object/instance

#

but if the method is static you didn't need to bring in the SliderScript object?

#

or are these tutorials just bad

coarse folio
azure pulsar
#

everything is an object so id avoid describing it as such. an instance is just the correct term

#

id say the tutorials are bad given what ive seen but thats only based on these static events

edgy jay
#

ok thanks for clarifying.

coarse folio
#

Must be pretty shit yea

edgy jay
#

Here is the one I was looking at

coarse folio
#

read the offical doc page on static to understand what it does

edgy jay
#

40 upvotes

#

ClickController.OnClickControllerEvent is just being used with no property on HealthController

coarse folio
#

BECAUSE ITS STATIC

azure pulsar
edgy jay
#

Got it. Thats what I wanted to make sure. I will definitely read the link you sent right now

#

I'm run into so much bad stuff...its driving me nuts...never mind the old stuff that is outdated.

#

I really appreciate all of you giving me input...it helps so much!

azure pulsar
#

a ton of stuff really isnt outdated because you should be learning the logic to use rather than just copy pasting the code. there could be rigidbody movement tutorials made 1-2 years ago that are "outdated" but in reality it uses rigidbody.velocity when unity 6 has updated this to be rigidbody.linearVelocity. the rest of the code and math behind such a tutorial would still be valid

#

this is a main reason i suggest to learn c# separately, so you aren't stuck on learning basics of coding while trying to learn unity