0

I have a script with a public function which sets up an animation trigger like this:

public class AnimationManager : MonoBehaviour {

    public Animator menuAnim;

    void Start () {
        menuAnim = GetComponent<Animator>();
    }

    public void Test() {
        menuAnim.SetTrigger("Fade");
    }
}

Now in a different gameObject i have another script that i want to simply call the function Test. So i made a script that did this:

public class Testing : MonoBehaviour {

   void begin(){
     AnimationManager.Test();
   // other stuff
   }
 }

But this leads to this error:

 An object reference is required to access non-static member `AnimationManager.Test()'

The error occurs in the first line of the begin function.

I am new ish to C# i originally learnt Javascript, so i am a bit confused how i would reference the member in order to call this function.

Hope you can help.

2 Answers 2

2

This won't really work since your AnimationManager class isn't static, you need to initialize it first like this:

AnimationManager someName = new AnimationManager();
someName.Test();

And take note they must have the same namespace, if not, you still need to add the namespace in the using directive.

Edited:

public static class AnimationManager : MonoBehaviour {

    public Animator menuAnim;

    static void Start () {
        menuAnim = GetComponent<Animator>();
    }

    public static void Test() {
        menuAnim.SetTrigger("Fade");
    }
}

This is how you're gonna call it:

public class Testing : MonoBehaviour {

   void begin(){
     AnimationManager.Test(); //since your AnimationManager class is already static
//you don't need to instantiate it, just simply call it this way
   // other stuff
   }
 }
Sign up to request clarification or add additional context in comments.

10 Comments

Is the new keyword required? It seems to work without, even so, i get this warning: never assigned to, and will always have its default value null i could ignore the warning but it suggests to me theres a better way to write it to avoid the warning.
you should put the new keyword... or if you don't like initializing everytime you're gonna call the class, just add the "static" keyword in the class BUT you should also make all methods/function static inside that class.. example: public static class AnimationManager
then you also need to add static void begin()
Hmm i see i can't work out which one i should be using. When and when not to use static. Does new remove the warning of always have its default value null
Infact using new causes this error You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed.
|
1

Basically you could use a static helper class to set things for the animator:

public class AnimationManager : MonoBehaviour {

    public Animator menuAnim;

    void Start () 
    {
            menuAnim = GetComponent<Animator>();
    }

    public void Test() 
    {
        AnimationHelper.Test(menuAnim);
    }
}

public class Testing : MonoBehaviour 
{
   void begin()
   {
    Animator menuAnim = GetComponent<Animator>();
    AnimationHelper.Test(menuAnim);
   }
}

public static AnimationHelper
{
    public static void Test(Anímation animation)
    {
        animation.SetTrigger("Fade");
    }
}

2 Comments

This is a bit confusing because public class Testing is a different script in my code or does that not matter? Curious to know the benefits of using a helper class?
Plus you seem to assigning the component to the SetTrigger which is suppose to receive a string. See my original code for SetTrigger to see the difference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.