- Home /
Is it possible to access a variable in a different script without reference?
Hello, I am asking this because I have seen no proof as to whether this is possible or not. Ok. so lets say I have a variable named dirt in "Script1" and I need to access it in "Script2". I know there are a few ways to do that like:
Script1.dirt; with dirt being static.
or:
gameObject.GetComponent().dirt; and dirt wouldn't have to be static.
what I want is to be able to have an "Info" script that holds information, like dirt. and then I would like to access it without the reference to dirt, like:
variable = dirt; instead of: variable = Info.dirt;
is this possible? I hope this post made sense, I'm not 100% sure how to ask this if it doesn't.
thank you for your help in advance.
What is the rationale behind wanting accessing a variable like that? It seems it would just make your code harder to understand and more error prone.
The only thing I can think of, is making your scripts inherit from the Info script, and making sure that dirt is public or protected.
class Info : $$anonymous$$onoBehaviour
{
protected float dirt = 5f;
}
class AnotherClass : Info
{
void Update()
{
Debug.Log("Dirt " + dirt);
}
}
Thank you, thank you, thank you! Worked beautifully! I never thought of doing it like that.
The reasoning behind it is, I am using it to store some variables that will literally NEVER change. I have had to declare these variables is multiple scripts, and now using this I can declare the variables is my "Info" script and use them wherever I need.
there for since the variables will never change I don't think it will cause any errors anywhere.
Its not a serious project, just something I'm doing to learn some different techniques.
Thank you so much @bobisgod234 !!
I am glad that it worked for you.
If you intend to have the value remain the same and never change, you can also declare it as a const.
protected const float dirt = 5f;
This will prevent you from accidentally assigning a different value to it.
$$anonymous$$ay I ask what uses this variable? If you want the value to be available to scripts throughout the game, than your original idea of having it as a static variable seems completely reasonable I$$anonymous$$O.
I have something like this in my own project:
public static class NetworkConstants
{
public static string serverURL = "blah";
public static int retryAttempts = 4;
//etc
}
@bobisgod234 is right! you can use protected const float dirt = 5f;
const keyword for variables which you don't want to change if you are initializing variables in the constructor then use Readonly
ins$$anonymous$$d of const. The other idea using Static is also good but if you declare
public static string serverURL = "blah"; //use static
with using static
keyword the memory will be reserved for that on the stack
so if you declare with const
, work with class name there will be no need of reference and memory reserved on heap
and it will be efficient.
public const string serverURL = "blah"; //use const
Answer by IgnoranceIsBliss · Jun 06, 2017 at 05:43 AM
You basically answered the question yourself by saying you know how to create a static.
This solution only works if you are going to have a single instance of the particular class (ie. 'Info') in your scene.
As long as you hold to that rule, you create a static member of the class that points to the first instance you created.
For example...
class MyRandomClass : MonoBehavior
{
public static MyRandomClass Instance;
void Start()
{
Instance = this;
}
}
When this behaviour starts up, the first thing it does is sets the static variable to point to itself. Then all other classes can interact via the MyRandomClass.Instance property.
Just remember - don't add any more of these 'Info' classes anywhere in the same scene - you'll break everything!
And if you'd like this object to move between scenes, you can always call DontDestroyOnLoad during that Start function. That will ensure that your object will survive between scene loads, which is ideal if you want this class to store your persistent game state.
This is th best method there is for your needs. And also you should change the code to if(Instance==null) Instance = this;
and if for debug purposes else Debug.Log("$$anonymous$$ore than one instance of $$anonymous$$yRandomClass is found")
I know how to use this method. I have used it countless times. As I stated earlier I was playing around trying to learn different methods of doing the same thing. Even if the method I am trying isn't the "best" I feel its still better to know them.
Why? What are you trying to do?
Even if the method I am trying isn't the "best" I feel its still better to know them.
But you aren't trying anything, you've come here looking for the best-practice way of doing what you want and it has been cited by this answer.
There are plenty of ways to do what you're "trying" to do:
Script2 writes to a text file and Script1 reads it.
Script2 posts its values to a central manager class and Script1 grabs it.
Script2 sends its data to a web server and Script1 pulls that down.
Script2 sends a text message to your mobile phone and then your write the value into Script1 in real time.
I didn't come here looking for the best-practice. I came here to see if something was possible. @bobisgod234 answered me in the comments and told me how to do it. That is what I was looking for, I don't need anything else.
Answer by ZackOfAllTrades · Jun 08, 2017 at 03:57 PM
You could inherit from the class like the previous example but then you can't extend Monodevelope. I suggest you just make the variable a constant.... public const dirt = 5f;