Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
0
Question by baturalpkabadayi · Jan 20 at 05:48 PM · script.unity 2dvariablesclassesaccess

How do I access a script from a class inside a script.

 public class ShopUpgrades : MonoBehaviour
 {
     [System.Serializable]
     public class Upgrade
     {
         public bool upgradeType {get; set;} // true == Click, false == Passive
         public string name {get; set;}
         public float upgrade_multiplier {get; set;}
         public int max_level {get; set;}        
         public int current_level = 0;
         public float price {get; set;}
         public bool isMax = false;
         public bool isAvailable {get; set;}
         
         public void UpgradeObject(ref GameObject upgrade_interface) {
             if (current_level != max_level) { //Increments the current level by one
                 current_level++;
             }
             GetComponent<Budget_tracker>().budget += upgrade_multiplier;
         }
     }
     
 }
 

In line 19 I want to access a script named "Budget_tracker" which is inside the same game object with the ShopUpgrades script. I need to modify "Budget_tracker"s variables in the "UpgradeObject" method of "Upgrade" class. I cannot do it in "ShopUpgrades" script because I also need to assign "Upgrade" objects in that script:

     public Upgrade someUpgrade = new Upgrade{
         upgradeType = true,
         name = "someUpgrade",
         upgrade_multiplier = 2,
         max_level = 5,
         price = 10,
         isAvailable = true,
     };
 
     public Upgrade someUpgrade2 = new Upgrade { 
         upgradeType = false,
         name = "UpgradeName2",
         upgrade_multiplier = 2,
         max_level = 5,
         price = 10,
         isAvailable = true,
     };
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Captain_Pineapple · Jan 20 at 08:37 PM

Unless you pass a reference to the ShopUpgrades object along when instantiating an Upgrade you simply don't.

A "normal" class which is not derived from a Monobehaviour has no concept of GetComponent.

So add a constructor to Upgrade and a private field to contain the ShopUpgrades class.

Comment
Add comment · Show 4 · Share
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
avatar image baturalpkabadayi · Jan 20 at 09:01 PM 0
Share

Could you demonstrate it a little bit on the code. I am really confused :(

avatar image Captain_Pineapple baturalpkabadayi · Jan 21 at 11:17 AM 0
Share

something like this:

 [System.Serializable]
 public class Upgrade
 {
     private readonly ShopUpgrade _shopUpgrade;
     public Upgrade(ShopUpgrade shopUpgrade)
     {
         _shopUpgrade = shopUpgrade;
     }
 
     public UpgradeObject(...)
     {
         //instead of GetComponent<Budget_tracker>() use:
         _shopUpgrade.GetComponent<Budget_tracker>().....
     }
 }
avatar image baturalpkabadayi Captain_Pineapple · Jan 21 at 11:33 AM 0
Share

Thank you so much that solves the problem

Show more comments

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

177 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 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 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 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 avatar image

Related Questions

Communication between classes from two different game objects 1 Answer

How Do I Access and Change Items in a List on Another Script? 2 Answers

Why is a variable not the same in the editor and script? 0 Answers

Abstracting class vars, accessing vars by variable 0 Answers

How to reassign a variable's class 1 Answer


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