I have two scripts: one that doesn’t inherit from anything (ANonMonoScript) and the other that inherits from MonoBehaviour (AMonoBehaviour).
Non-Monobehaviour:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ANonMonoScript
{
public static void AMethod()
{
// irrelevant code
// <= here i want to call a method from the AMonoBehaviour MonoBehaviour script.
}
MonoBehaviour:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AMonoBehaviour : MonoBehaviour
{
public Button button;
public Sprite ASprite;
void Awake()
{
}
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void AMethodToCall()
{
button.image.sprite = ASprite;
}
}
The MonoBehaviour is attached to the Button UI GameObject. I’m trying to change the Target Graphic of the UI button to the image that i have attached to the ASprite in the Inspector when AMethodToCall() is called from the AMethod() of the ANonMonoScript.
However, i don’t know how to call the MonoBehaviour method from the static non-Monobehaviour.
It is not clear from where you call the static method or what is the point of the design but if you want to use instance members anywhere outside of the class itself (or classes that inherit from it) you need a reference.
public static AMethod(AMonoBehaviour aMonoBehaviour)
{
aMonoBehaviour.AMethodToCall();
}
Thank you for your answer, @PizzaPie ! Seems that i have made quite a typo in my original post. I’m trying to call a MonoBehaviour method from a static non-MonoBehaviour, not to call a static non-MonoBehaviour from a MonoBehaviour . Sorry, my bad, corrected the wording in the post! The question still stands :).
Doesn’t really matter, you ll always need a reference to the target object, how to obtain that reference depends on the overall structure of your system. I can’t really provide any insight given the lack of info on the intended usecase.
Regardless, thank you for your help! There isn’t much to the structure in this particular case other than script A and script B, with script B (the MonoBehaviour) being attached to the Button GameObject.
Providing some extra details that might help (just realized what @PizzaPie meant when they said “the overall structure of your system”):
The MonoBehaviour method, that i’m trying to call from a static non-MonoBehaviour method is in a script that is attached to a Button UI Object, which is a child object of Canvas UI Object. The ASprite is a PNG image that i have attached via Inspector.
creating a custom tag for the Button GameObject (AButtonTag).
creating a reference of the script type that i’m trying to access.
finding the GameObject by tag and using GetComponent to access the script in question.
Non-Monobehaviour:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static AMonoBehaviour aMono;
public static class ANonMonoScript
{
public static void AMethod()
{
aMono = GameObject.FindGameObjectsWithTag("AButtonTag")[0].GetComponent<AMonoBehaviour>();
// irrelevant code
aMono.AMethodToCall(); // <= here i call a method from the AMonoBehaviour MonoBehaviour script
}
MonoBehaviour:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AMonoBehaviour : MonoBehaviour
{
public Button button;
public Sprite ASprite;
void Awake()
{
}
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void AMethodToCall()
{
button.image.sprite = ASprite;
}
}
I’ve also found a question, that is similar to mine (which essentially resolved my problem), might be helpful to some:
I don’t understand why you said you need a reference as you can utilize most of the methods statically. Not the most useful example as I am calling print but it could be more useful with a spawn item command where you need to instantiate.
public class Commands
{
public static void EnableRagdoll(GameObject gObj) {
MonoBehaviour.print("Print");
foreach(var child in gObj.GetComponentsInChildren<Rigidbody>()){
child.isKinematic = false;
}
}
}
// Usage
Commands.EnableRagdoll(gameObject);