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 UnityAlexV · Feb 16, 2016 at 03:36 AM · c#public variableaccessing from any scriptaccessing scripts

How to Reference Other Scripts' Variables in C#

Ok mods... before you delete this, hear me out.

I have looked for a solution to this. And I looked again. And I know there are many answers. And there are even more answers.

But every answer I have found does nothing to help.

I have a script:

 using UnityEngine;
 using System.Collections;
 
 public class GlobalMoney : MonoBehaviour {
 
     public float money;
 
 }

...and I need (well obviously not need, but want) to access the money variable in another script:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class MoneyDisplay : MonoBehaviour {
 
     GameObject dismaster = GameObject.Find("Master");
     GlobalMoney moneycomp = dismaster.GetComponent<GlobalMoney>();
     float dismoney = moneycomp.money;
     public Text TextComponent;
 
     void Update () {
         TextComponent.text = "$" + dismoney;
     }
 }

I get this error:

 Assets/MoneyDisplay.cs(8,33): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `MoneyDisplay.dismaster'

..and this one:

 Assets/MoneyDisplay.cs(9,26): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `MoneyDisplay.moneycomp'

Any help will be appreciated. If I have come off as rude, I apologize sincerely, but I am quite frustrated as I have been looking and looking for a few hours how to do such a simple task, to no avail.

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 EmHuynh · Feb 16, 2016 at 04:10 AM

Hello, @UnityAlexV.

To resolve the errors, try initializing dismaster, moneycomp and dismoney through the Start or Awake functions, whichever suits the purpose of your script. Here's the fixed version of MoneyDisplay.cs:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class MoneyDisplay : MonoBehaviour {
 
     GameObject dismaster;
     GlobalMoney moneycomp;
     float dismoney;
     public Text TextComponent;
 
     void Start() {
         dismaster = GameObject.Find("Master");
         dismoney = moneycomp.money;
         moneycomp = dismaster.GetComponent<GlobalMoney>();
     }
 
     void Update () {
         TextComponent.text = "$" + dismoney;
     }
 }


Here is a video to explain the Start and Awake functions (click this link). Let us know if you have any questions!

Comment
Add comment · Show 5 · 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 UnityAlexV · Feb 16, 2016 at 04:27 AM 0
Share

Thank you kindly, but I have gotten the following error: UnityException: You are not allowed to call this function when declaring a variable. $$anonymous$$ove it to the line after without a variable declaration. Said error does not have a line or space, it simply is. Help would be appreciated.

avatar image EmHuynh UnityAlexV · Feb 16, 2016 at 04:47 AM 0
Share

@Unity$$anonymous$$V, that's weird. I tested the script and it worked perfectly. Let's initialize dismaster in the Start function.

avatar image UnityAlexV EmHuynh · Feb 16, 2016 at 04:56 AM 0
Share

Nothing changed. Any more ideas?

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

100 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

Related Questions

Determine Which Switch Case Was Called Last C# 1 Answer

Change public variable with button 1 Answer

Trying to give instantiated object a value 0 Answers

Creating Modular Firing Mechanism 1 Answer

Setting value in inspector does not update script. 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