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
5
Question by Edy · May 29, 2010 at 07:55 PM · properties

How do I create public read-only properties?

I want my script to publish some variables at runtime that could be read by other scripts, but they cannot modify them. Also I don't want these variables to appear at Unity's editor.

Thank you!

Comment
Add comment · Show 3
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 Cyclops · May 30, 2010 at 12:55 AM 1
Share

@Edy, I was about to suggest you also upvote the answer you checkmarked - but then I remembered it takes 15 points to upvote, at least according to the FAQ. Wups.

avatar image Cyclops · May 30, 2010 at 04:03 PM 1
Share

Okay, now you can upvote him. :)

avatar image Edy · May 30, 2010 at 07:08 PM 1
Share

Thank you for the suggestion. I'm new to this great system, and I'm still getting used to it. Time to read the FAQ I guess :)

3 Replies

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

Answer by Mike 3 · May 29, 2010 at 08:10 PM

best way is to either use functions or properties

functions will work in js and c#:

(js)

var test : float = 1;

function getTest() : float { return test; }

(c#)

float test = 1;

public float getTest() { return test; }

the alternative is properties in c#:

float test = 1;

public float Test { get { return test; } }

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 Edy · May 29, 2010 at 09:30 PM 0
Share

Thanks to both of you! I've marked this one just because it's most complete, and also I'm currently using javascript (sorry, I forgot to specify that in the question)

avatar image
0

Answer by Cyclops · May 29, 2010 at 08:08 PM

You didn't say what language, so I'll assume C#. There may be other ways, but the only one I can think of - is to use a Property, and define a get function, but not a set function.

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 Edy · May 30, 2010 at 10:27 PM

I've just discovered that you can actually access the private variables in one script from other script! You can use them exactly as if they were public variables (at least in Javascript, didn't tested in C#).

I guessed that the 'private' keyword would keep the variable isolated from other scripts, but it's more like a "non-publish-in-editor" tag.

Edit: You can access the private variables only when accessing the script this way:

var myScript = Vehicle.GetComponent(myScriptName);
var value = myScript.privateValue;
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 Mike 3 · May 30, 2010 at 11:34 PM 0
Share

It depends how you access the script - if you reference it with dynamic typing (e.g. GetComponent(moo).variable), it'll get the member data with reflection (which is slow). In c# you can't really do that without doing the reflection yourself, which is a lot more work

avatar image Edy · Jun 01, 2010 at 10:50 PM 0
Share

Ok, thanks! I think I'll go the straight way and use the getter functions :)

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

No one has followed this question yet.

Related Questions

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

Properties names, same as class okay or bad? 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Material doesn't have a color property '_Color' 4 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