Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 gregzo · Dec 08, 2011 at 11:11 PM · destroyvariablesstoring

Storing variables in a separate script

Hi!

I'm cleaning up my code, and realize I have a bunch of options. Here is the situation: I have an object which can have 3 different behaviours, coded in 3 different scripts. So far, changing behaviour means adding the new script, passing on a bunch of variables, and destroying the now unneeded script. Would it be bad practise to store all these variables in a "properties" script that would be cached first thing when a new script is loaded?

I'm an OOP rookie, loving it so far but sometimes baffled by the fact that there are so many ways to do things...

Thanks for your advice!

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

3 Replies

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

Answer by dannyskim · Dec 08, 2011 at 11:29 PM

There are several ways to do this. The most basic and easiest way would be to create a singleton object manager that holds the static variables for all the behaviors, and hold all these values for all three different scripts rather than sharding them out in each script, just as you suggest in your question. So no, this is actually a normal practice, it's not bad to do this. If anything, it's more efficient to do so, because you're programming in an "Object Oriented" manner, passing information to and from objects that are set to do a specific job that you intend them to.

Caching copies of a script is fine and dandy, and sometimes you have to do this based on the situation, but for this scenario an variable manager singleton would be a better choice.

I'm also a programming novice, and once you get further down the line in programming you'll discover that there are more and more options for doing something like this, and much more efficient ways about handling these events.

This is where class inheritance starts to arise, when you need to manage objects that are similar in type and share a lot of the same properties. This is referred to as a base class. An example would be an "Animal" as a base class, and a sub-base class that extends from Animal would be Reptile, or Mammal. So in code thoughts, this class would be declared as:

 public class Reptile : Animal{}

And further down the line:

 public class Lizard : Reptile{}

There can only be one base class, but there are several tiers of inheritance that all extend from the base class. So, in future cases you may want to use this ( as it's considered one of the core principles of object oriented programming and .net in general ) and study it. A good starter link would be:

http://www.csharp-station.com/Tutorial/CSharp/Lesson08

or any of there links on that site are good places to start learning about this.

Another way you handle these changes in variables would be delegates, events, and actions. Prime31 has some pretty good basic tutorials on getting you started with these:

http://www.youtube.com/user/prime31studios

Look for the videos with 'coding tips' in the titles, specifically the tutorials on extensions and actions, as well as the Linq tutorials which shows examples of creating delegates, events, and listeners.

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 gregzo · Dec 08, 2011 at 11:47 PM 0
Share

Thanks very much for the detailed answer. I'm using UnityScript though, not C#, so I guess no delegates for me yet... About the variable manager singleton, is it doable in UnityScript?

avatar image dannyskim · Dec 09, 2011 at 12:01 AM 0
Share

Yep it is, look at jahroy's answer for that below this comment.

avatar image
2

Answer by jahroy · Dec 08, 2011 at 11:30 PM

Look into ScriptableObjects.

They allow you to store all kinds of data, they get serialized, and they don't have to be attached to GameObjects. You can assign them to your scripts like variables, too.

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 gregzo · Dec 09, 2011 at 12:01 AM 0
Share

Thanks! I'll check it out, but unsure if they'd be suitable for my current problem: I need some of these varaiables to show up in the inspector (basicaly, I have "soundObjects" so when I add one to the scene, I specify in the inspector its audioClip as well as other stuff, which 2 different behaviours need to access...)

avatar image jahroy · Dec 09, 2011 at 10:06 PM 0
Share

You can definitely edit ScriptableObjects in the Inspector.

We use them to hold all the configuration data for or GUI (for example the size and location of our buttons).

This way we can edit all the data in the Inspector and keep a separate instance of the GUI Data Object for each platform we want to build our game for.

When we switch platforms, we just switch the gui data object assigned to our GUI controller to match the new platform.

avatar image
0

Answer by Oninji · Dec 08, 2011 at 11:31 PM

It would depends by what scripts these variables are used.

If a variable is used only by a single script, you should alway store it in that same script. Other than uneedlessly calling for another script.

But if a lot of different scripts depends on them, it could be a good idea. As long as you comment in the scripts that need these variable, where to fetch them.

Also, if a variable is constantly relied upon by numerous scripts. A good practice is to call them as "static", as it make them freely available to any scripts in your build.

Comment
Add comment · Show 1 · 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 gregzo · Dec 08, 2011 at 11:51 PM 0
Share

Well, my vars cant be static (they are properties of an object class of which many instances coexist). And the idea is to store in a separate script only variables shared by the different behaviours, so behaviour specific vars wouldn't be cached for nothing.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to store the position of an object at the start of a scene 1 Answer

How do you return variables from other scripts? 2 Answers

accesing variable from other script 4 Answers

Error when accessing variables from another script: NullReferenceException: Object Reference not set to an instance of an object.. 0 Answers

Why can't i acsess this variable 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