Answers for "script communication in c#"

C#
1

script communication in c#

//lets bring a value from Script A to Script B
    //DONT FORGET TO MAKE EVERYTHING PUBLIC,I MADE PUBLIC
    public class Script_A
{
    Script_B scriptB;//getting a reference to other script
    public int number = 10;
    void Start()
    {   /*if you dont want a public reference use this code below*/
        scriptB = GameObject.Find("example").GetComponent<Script_B>();
        //!.Way:Pass the parameter
        scriptB.SayNumber(number);/*calls the method and
                                gives the number value*/
    }
    //3.Way:return value
    public int Number()
    {
        return number;
    }
}
public class Script_B
{
    Script_A scriptA;//getting a reference to other script
    public void SayNumber(int number)
    {
        Debug.Log(number);
    }
    void Start()
    {
        //calling return type function
        Debug.Log(scriptA.Number());
        //2.Way:Getting direct access to other values
        Debug.Log(scriptA.number);
    }
}
Posted by: Guest on December-31-2020

C# Answers by Framework

Browse Popular Code Answers by Language