• 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 Ryanvanpolen · May 09, 2014 at 09:29 AM · variablesreferenceextend

How to make an central script with viriables

Hi, I'm Ryan and I would like to make a central script with all my variables stored in it. The problem is how do I make such a script?

I tried extend, but then I had to attach all variables to every other script. Does anybody knows how to make my central script work? Thanks a lot...

Comment
Add comment · Show 2
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 Ekta-Mehta-D · May 09, 2014 at 09:35 AM 0
Share

Then make such script and add to one of the gameobject. whenever needed thaoes variables , access via that gameobject.

avatar image Owen-Reynolds · May 09, 2014 at 02:05 PM 0
Share

$$anonymous$$aking a script with a bunch of variables is the easy part. I think most people just attach it to an empty gameObject, and don't bother with static. Then maybe take a look at "how to find variables in other scripts." The manual has a page on it, plus lots of Q's in here.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by iamvishnusankar · May 09, 2014 at 09:37 AM

Create a static instance of your central script

 public class MyMainScript : MonoBehaviour 
 {
     //Declare a static instance of this script
     public static MyMainScript instance;
 
     //Decalre all vaiables as public variables
     public int x=0;
     public float y=1.0f;
 
         
     //Asiign the static instance on Awake()
     void Awake()
     {
         instance = this;
     }
 
     
 }

Access any variables of your central script from any other script using

 int a = MyMainScript.instance.x;
 float b = MyMainScript.instance.y;
 
Comment
Add comment · Show 2 · 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 Ryanvanpolen · May 09, 2014 at 09:42 AM 0
Share

Thanks, but I work in uJS, so this is a little bit confusing... Does anybody knows how to do it in JavaScript?

avatar image Ekta-Mehta-D · May 09, 2014 at 09:47 AM 0
Share
 public  static var  instance : $$anonymous$$y$$anonymous$$ainScript;
 
 //Decalre all vaiables as public variables
 public var x : int =0;
 public var y : float =1.0f;
 
 
 //Asiign the static instance on Awake()
 function Awake()
 {
    instance = this;
 }

for accessing :

 var a : int = $$anonymous$$y$$anonymous$$ainScript.instance.x;
 var b : int = $$anonymous$$y$$anonymous$$ainScript.instance.y;

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Error message "variable not assigned" ? 1 Answer

Having a script variable update between two objects 1 Answer

PDF scripting reference? 1 Answer

Referencing booleans from settings 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