Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by SeanSabe · Feb 20, 2016 at 04:53 PM · variablesclassinstancemethod

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

44 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges