'How can I call a return value from ScriptA.cs in ScriptB.cs?

So I have a button that has to be pushed and when the button is pushed the light is turned on. Can I take a return Boolean value from a function in ScriptA.cs and then call the return value in ScriptB.cs ?



Solution 1:[1]

Do you mean something like the following codes?

public class ScriptA : MonoBehaviour
{
    private bool ReturnRandomBool()
    {
        int randomInt = Random.Range(0, 2);
        return randomInt == 0;
    }
    
    void Start()
    {
         ScriptB.Instance.DoSomeThingBasedOnValue(ReturnRandomBool());
    }
}

|

public class ScriptB : MonoBehaviour
{
    public static ScriptB Instance;

    private void Awake()
    {
        Instance = this;
    }

    public void DoSomeThingBasedOnValue(bool val)
    {
        // do something
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abolfazl