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 roblewisppc · Mar 31, 2020 at 08:39 PM · global variables

Can't get global variables to work (reference variable from one script to another),Cannot Reference Variable from other Scripts

I am sorry for asking this old chestnut of a question. I've read article after article and I simply cannot figure it out. I am sorry.

I have two scripts and I need to reference a variable from another script. Sounds basic and there are countless articles. But it doesn't seem to work (why oh why can't Unity just create a UI global variable list???).

Anyway. Here are my scripts. It's for an RTS strategy building placement.

Buildings are placed, and each building has a unit of energy used on build (I call the energy WorldCore).

I have a maximum WorldCore (MaxWorldCore) variable. This is set as follows in my main script:

 public class WorldCoreHandler : MonoBehaviour {    
     
     public int WorldCoreMax = 5000;  // the max worldcore value available}

All good. I can add my WorldCoreMax to a text/graphical UI if I want (within this script), and all is well.

My problem stems when buildings are placed, or code is written in other scripts which need to reference the WorldCoreMax variable. I get the classic "the name 'WorldCoreMax' does not exist in the current context"

So I tried to be clever and make the WorldCoreMax variable public as per my script. The thinking being I can call on the WorldCoreMax from other scripts. As per what I have read online.

Really I just want to learn how to print a message to the console from one script to another, confirming the link between scripts. So on my second script, when my building is placed, I add the following:

 print(WorldCoreMax);

You got it - it doesn't fire. I get the the "name 'WorldCoreMax' does not exist in the current context" error message.

There are so many articles online but they don't really give a basic overview of:

1 - how to set the original variable as a global in all cases 2 - how to reference it in the second.

I have looked and followed but I can't figure it out. I guess I really just need the actual script with my setup, so I can see how it works and then I can go from there.

Thank you so much for your help!

,I am sorry for asking this old chestnut of a question. I've read article after article and I simply cannot figure it out. I am sorry.

I have two scripts and I need to reference a variable from another script. Sounds basic and there are countless articles. But it doesn't seem to work (why oh why can't Unity just create a UI global variable list???).

Anyway. Here are my scripts. It's for an RTS strategy building placement.

Buildings are placed, and each building has a unit of energy used on build (I call the energy WorldCore).

I have a maximum WorldCore (MaxWorldCore) variable. This is set as follows in my main script:

 public class WorldCoreHandler : MonoBehaviour {    
     
     public int WorldCoreMax = 5000;  // the max worldcore value available
     public int WorldCoreTally = 0;  // the amount of worldcore used up so far

}

All good. I can add my WorldCoreMax to a text/graphical UI if I want (within this script), and all is well.

My problem stems when buildings are placed, or code is written in other scripts which need to reference the WorldCoreMax variable. I get the classic "the name 'WorldCoreMax' does not exist in the current context"

So I tried to be clever and make the WorldCoreMax variable public as per my script. The thinking being I can call on the WorldCoreMax from other scripts. As per what I have read online.

Really I just want to learn how to print a message to the console from one script to another, confirming the link between scripts. So on my second script, when my building is placed, I add the following:

 print(WorldCoreMax);

You got it - it doesn't fire. I get the the "name 'WorldCoreMax' does not exist in the current context" error message.

There are so many articles online but they don't really give a basic overview of:

1 - how to set the original variable as a global in all cases 2 - how to reference it in the second.

I have looked and followed but I can't figure it out. I guess I really just need the actual script with my setup, so I can see how it works and then I can go from there.

Thank you so much for your help!

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

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

How to use objects differently between scenes? 0 Answers

How to most efficiently detect a changed variable and then act on it? 1 Answer

Global variables are null within functions? 0 Answers

Any reason why global vars are null within certain functions? 0 Answers

automatic levelling out airplane when near the ground? 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