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 Peysbubby · Jun 06, 2017 at 12:19 AM · c#variablesreferencesinfo

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.

Comment
Add comment · Show 4
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 bobisgod234 · Jun 06, 2017 at 01:44 AM 1
Share

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);
     }
 }
avatar image Peysbubby bobisgod234 · Jun 06, 2017 at 03:19 AM 0
Share

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 !!

avatar image bobisgod234 Peysbubby · Jun 06, 2017 at 05:00 AM 0
Share

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
 }
avatar image SohailBukhari · Jun 06, 2017 at 05:40 AM 0
Share

@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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 4 · 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 KKS21199 · Jun 06, 2017 at 05:49 AM 0
Share

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")

avatar image Peysbubby · Jun 06, 2017 at 03:54 PM 0
Share

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.

avatar image TreyH Peysbubby · Jun 06, 2017 at 04:18 PM 1
Share

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.

avatar image Peysbubby TreyH · Jun 06, 2017 at 04:21 PM 0
Share

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.

avatar image
0

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;

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

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Variable set twice on the same Update call - Second time doesn't update? 2 Answers

How do I make a list of references to a variable? 2 Answers

why does my object reference go out of scope? 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