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 /
avatar image
0
Question by YungxSlavy · May 26, 2019 at 09:53 PM · classesaccessing

Accessing a Class From Another Script

This is probably a foolish question but is there a possibility to access a class from another script? I know you can attach it to a gameobject then get the script from the component. But for my case I'm using a class that holds my coins so that they can be saved. The saving script accesses the file no problem. But when I apply the same logic to another script it gives me an error " 'somescript' is never assigned to, and will always have its default value null" public class CoinTracker { private int _coins; public CoinTracker () { } public CoinTracker(int coins) { _coins = coins; } public int coins { get { return _coins; } set { _coins = value; } } } And then the "SomeScript:MonoBehaviour" is like this:

 {
     
     private CoinTracker coinTracker;
 
 
     // Start is called before the first frame update
     void Start()
     {
         
        
 
         Debug.Log(coinTracker.coins); 
 
     }
 
  
 }
 

I am just really confused because the "Saving" script accesses the "Cointracker" class just like it is shown in the "SomeScript". Any light or possible ways to access the class directly if possible would be awesome. Thank you!!

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

2 Replies

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

Answer by highpockets · May 26, 2019 at 10:43 PM

 public class MyClass : MonoBehaviour
 {
 //lots of fun stuff in this class
 }
 
 

Above is the script you want to access from another script, which is below:

 public MyOtherClass : MonoBehaviour
 {
 
 [SerializeField] MyClass firstScript;
 
 }

Now in the inspector, just click on the little circle to the right of the field and it will show you the instances of the type your looking for if they exist and you put it in there.

Comment
Add comment · Show 3 · 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 YungxSlavy · May 26, 2019 at 10:54 PM 0
Share

Thanks! One question, would I use that same method if "$$anonymous$$yClass" wasn't $$anonymous$$onoBehaviour? Or would I use another way?

avatar image highpockets YungxSlavy · May 27, 2019 at 05:49 AM 1
Share

Same way. $$anonymous$$onoBehaviour classes just have more capabilities in unity, but you don’t need to use them if you don’t need those abilities

avatar image YungxSlavy highpockets · May 27, 2019 at 09:03 PM 0
Share

Great thank you so much for your help!!

avatar image
0

Answer by Zankomag · May 27, 2019 at 10:57 PM

In your case you had better use Singleton for CoinTracker if you want only one instance of this class:

 public class CoinTracker
 {
     public static CoinTracker Instance { get; private set; } = new CoinTracker();
 
     private CoinTracker(){ }
 
     public int Coins { get; set; }
 }

Now you can get Coins from anywhere and without assigning it in Inspector every time:

 public class SomeScript : MonoBehavior{
         void Awake(){
                print(CoinTracker.Instance.Coins);
         }
 }
Comment
Add comment · 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

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

108 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

Related Questions

should class members ever be static? 1 Answer

How to select a variable from another GameObject by accessing the class in the Inspector. 2 Answers

Best programming practice for making in game menus 0 Answers

Accessing an object through a direct reference or a pointer 1 Answer

How do I change a variable in Script A from inside Script B 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