Using variables from another script.
Greetings.
I have these codes:
GameManagementScript.cs
public class GameManagementScript : TerrainSelectionScript {
public Canvas hudCanvas;
public Button createButton;
public Toggle[] resourcesToggle = new Toggle[5];
public RawImage[] infoBox = new RawImage[5];
public SpawnRaycast raycastSpawnScript;
int houseIncome, buildingIncome, apartmentIncome;
int houseConsumption, buildingConsumption, apartmentConsumption;
public Text demand, cash, production;
void Awake()
{
houseConsumption = 3;
houseIncome = 4;
buildingConsumption = 20;
buildingIncome = 30;
apartmentConsumption = 5;
apartmentIncome = 6;
hudCanvas.gameObject.SetActive(true);
for (int i = 0; i < infoBox.Length; i++)
{
infoBox[i].gameObject.SetActive(false);
}
}
void GameStats()
{
//raycastSpawnScript.Construir();
//Debug.Log(raycastSpawnScript.numberOfHouses + ", " + raycastSpawnScript.numberOfBuildings + ", " + raycastSpawnScript.numberOfApartments);
demand.text = "Demanda: " + ((raycastSpawnScript.numberOfApartments * apartmentConsumption) +
(raycastSpawnScript.numberOfBuildings * buildingConsumption) +
(raycastSpawnScript.numberOfHouses * houseConsumption));
cash.text = "Crédito: " + ((raycastSpawnScript.numberOfApartments * apartmentIncome) +
(raycastSpawnScript.numberOfBuildings * buildingIncome) +
(raycastSpawnScript.numberOfHouses * houseIncome));
}
void Update()
{
Invoke("GameStats", 5);
}
public void SelectingResource()
{
for (int i = 0; i < resourcesToggle.Length; i++)
{
if (resourcesToggle[i].isOn)
{
Debug.Log("El recurso seleccionado es " + resourcesToggle[i].name);
}
}
}
SpawnRaycast.cs
public class SpawnRaycast : MonoBehaviour {
Vector3 puntosalida;
public int frecuenciaCreacion;
float sizeX;
float sizeZ;
public GameObject[] suburbsHouseArray, desertHouseArray, mountainHouseArray, apartmentArray, officialBuildingsArray;
[HideInInspector]
public int numberOfBuildings, numberOfHouses, numberOfApartments;
void Start()
{
//InvokeRepeating( "Construir", 0, frecuenciaCreacion);
sizeX = transform.localScale.x / 2;
sizeZ = transform.localScale.z / 2;
}
void Update()
{
//Debug.Log(numberOfHouses + ", " + numberOfBuildings + ", " + numberOfApartments);
//Construir();
}
void Awake()
{
suburbsHouseArray = Resources.LoadAll<GameObject> ("Buildings/suburbsHouse");
desertHouseArray = Resources.LoadAll<GameObject> ("Buildings/desertHouse");
mountainHouseArray = Resources.LoadAll<GameObject> ("Buildings/mountainHouse");
apartmentArray = Resources.LoadAll<GameObject> ("Buildings/apartments");
officialBuildingsArray = Resources.LoadAll<GameObject> ("Buildings/officialBuildings");
}
public void Construir()
{
puntosalida = transform.position;
puntosalida = transform.position;
puntosalida.x += Random.Range (-sizeX,sizeX);
puntosalida.z += Random.Range (-sizeZ, sizeZ);
RaycastHit hit;
Ray landingRay = new Ray (puntosalida, Vector3.down);
if(Physics.Raycast(landingRay, out hit))
{
if (hit.collider.tag != "BuildingPrefab" || hit.collider.tag != "CoastZone")
{
if (hit.collider.tag == "Apartment")
{
Instantiate(apartmentArray[Random.Range(0,2)], hit.point, Quaternion.Euler(0f, 180f, 0f));
numberOfApartments++;
}
else if (hit.transform.tag == "Suburbs")
{
Instantiate(suburbsHouseArray[Random.Range(0,3)], hit.point, Quaternion.Euler(0f, 180f, 0f));
numberOfHouses++;
}
if (hit.collider.tag == "Desert")
{
Instantiate(desertHouseArray[Random.Range(0,3)], hit.point, Quaternion.Euler(0f, 180f, 0f));
numberOfHouses++;
}
else if (hit.transform.tag == "Official")
{
Instantiate(officialBuildingsArray[Random.Range(0,2)], hit.point, Quaternion.Euler(0f, 180f, 0f));
numberOfBuildings++;
}
if (hit.collider.tag == "Mountain")
{
Instantiate(mountainHouseArray[Random.Range(0,1)], hit.point, Quaternion.Euler(0f, 180f, 0f));
numberOfHouses++;
}
}
}
}
}
The problem is that I'm trying to access the variables "numberOf" in GameManagement from SpawnRaycast but isn't working. I don't know if I'm missing something. Also, if I tried to call the method Construir() using rayscastSpawnScript.Construir() and ain't working either.
Since C# does not allow multiple base classes I'm tring to call methods and variables from other script using what I'm showing you. The base class "TerrainSelection" is for another thing I'm doing but it does not interfere with the issue , so don't pay attention to it.
Any idea mates?
Your answer
Follow this Question
Related Questions
[C#] Instantiating via class references (Need help understanding) 1 Answer
Method not being called from another script 1 Answer
How can I access both classes from a single type? 1 Answer
Can someone explain this to me? Player myPlayer = new Player(); 1 Answer
Inheriting values from base class in c# 0 Answers