script communication in c# code example
Example: 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);
}
}