- Home /
Trying to access functions and variables in from one script in another.
Okay so i have a simulation that i am building that has to deal with population. I have a main script that is holding the population of the world and the individual types of population that there are. I have another script that is attached to each building that manages the population of that building and then wants to send an update to the main script with the change in population. The issue here is that I want to use on script to manage all the population types. I have a public function in the main script that will do the population update and sort by type and I want to occupancy script to just call the function and then update the population on the main script. Note main script is static and occupancy script is not. So here is a small code sample.
Main Script public int lowResPopulation; public int midResPopulation; public int hiResPopulation; public int PopulationChange (string tenant, int level, int change) { int noReturn = 0;
if (tenant == "r")
{
switch (level)
{
case 1:
lowResPopulation = lowResPopulation + change;
return lowResPopulation;
case 2:
midResPopulation = midResPopulation + change;
return midResPopulation;
case 3:
hiResPopulation = hiResPopulation + change;
return hiResPopulation;
default:
Debug.Log("PopulationChange pssed wrong int level type");
return noReturn;
}
}
Occupancy Script string tenantType; int tenantLevel; int change;
Main.Instanse.PopulationChange(tenantType, tenantLevel, change)
I thought about using extern but I keep getting an error telling me I'm not using it right. How does one ensure that when this function runs in the occupancy script that it updates the variable I want in the main script and not in the occupancy script itself? Let me know if you need any more information.
Answer by tanoshimi · Feb 06, 2016 at 11:03 AM
I recommend you read up about singletons. Here's one place to start: https://unity3d.com/learn/tutorials/projects/2d-roguelike-tutorial/writing-game-manager