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!
Your answer
Follow this Question
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