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 PixelMuncher · Aug 10, 2011 at 05:19 AM · c#editor-scriptingeditorwindowvaluesaccessing

Setting variables in public class to values from editor script

So I created my first Editor script. The script opens up a window with an object field and allows me to make several changes to the object through toggle switches and EnumPopups. I click a button and it applies all the attributes I set to the object I placed in the object field. It works like a charm, however the editor script performs some calculations based on the attributes I set that I would like to have access to in the script that is applied to the object.

Essentially, what I would like to be able to do is have another class that has variables to hold the values that my Editor script calculated so that the script that is applied to the object can have access to these values without having to do the calculations itself.

For example, my editor script will have something like this:

 //some calculation
 private float CalcSomething()
 {
     someCalculation = //perform some calculation
     return someCalculation;
 }
 //this function gets called when I press the button to set the objects attributes
 private void SetObjProperties()
 {
      ObjProperties.mCalc = CalcSomething();
 }

Then in my ObjProperties.cs

 public class ObjProperties
 {
      //container for calculation from the editor script
      public float mCalc;
 }

Then some other MonoBehaviour script that is actually applied to the object can have access to this value by saying something like:

 float someVal = ObjProperties.mCalc;

Problem is, this doesn't work. mCalc always has it's default value of 0.0f.

What am I doing wrong?

Any help is greatly appreciated. This is driving me crazy.

Comment
Add comment · Show 1
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 Bovine · Aug 10, 2011 at 07:12 AM 0
Share

You say always - have you stepped through the code to see whether mCalc ever has a value other than 0.0f? Also, remember that in your pseudocode above objProperties is a reference type and not a value type, so if you're passing it around and not copying it, you probably want to start making a copy in SetObjProperties(). Naturally if you zero mCalc somewhere after you've returned it then it's going to change.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by PixelMuncher · Aug 10, 2011 at 01:36 PM

After lots of poking around I finally found what I needed here: The skinny on ScriptableObjects. This method lets me store all my precomputed data in an asset file that I can access through other scripts, which is exactly what I want.

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 Kilometers · Jan 27, 2012 at 05:53 AM 0
Share

The link you provided seems to be dead now. If you ever do return and find this comment, perhaps you could share the gist of that link's info. +1 for finding the answer though (and to hopefully get your attention, :P )

avatar image
1

Answer by PixelMuncher · Jan 27, 2012 at 07:45 AM

Certainly Kilometers. First you will need to create a class that extends ScriptableObject (make sure you have using UnityEngine at the top) for your stored variables. It should look something like this:

 public class StoredData : ScriptableObject
 {
       public float myStoredFloat;
 }

Then, in your editor script have some method you call such as this one.

 private void StoreData()
 {
     //create an instance of your StoredData class
     StoredData storedData = (StoredData)ScriptableObject.CreateInstance("StoredData");
     //create an asset file at given path
     AssetDatabase.CreateAsset(storedData, "Assets/storeddata.asset");
     //this basically tells Unity that our scriptable object variable has changed and needs to be saved
     EditorUtility.SetDirty(storedData);
     //finally assign value to stored variable
     storedData.myStoredFloat = 1f;
 }

What this will do is create an asset file that you can reference through other scripts and thus have access to your stored values. So somewhere in your MonoBehaviour have:

 public StoredData storedData;

and simply drag the storeddata.asset file into the object field in the inspector.

That pretty much sums it up. If you have any further questions on how this works just let me know.

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

Answer by Waz · Aug 10, 2011 at 01:24 PM

You can't store properties just as class variables (I'm assuming you mistranscribed a static, since your code wouldn't even compile otherwise) and expect them to persist. They'll be trashed if that class is reloaded, and obviously will be lost on restart.

Simplest would be to store the calculated value in the actual scene object that ultimately needs it, not try to poke it elsewhere.

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 PixelMuncher · Aug 10, 2011 at 01:39 PM 0
Share

Yeah, my pseudocode was a bit rushed. ^_^

I found what I needed and posted it as an answer. I tested it and it works. Thanks for your time though.

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

Multiple Cars not working 1 Answer

Unity Editor - Create a custom scene view 0 Answers

Custom Editor window loses values on play. 1 Answer

How do I create custom editor window layout through script ? 0 Answers

Distribute terrain in zones 3 Answers


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