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 ChristianLinnell · Dec 31, 2017 at 02:22 AM · inspector

How can I create an inspector that lets me select another script's variable.

Say my game has several stats like health, hunger, and thirst.

I want to create a prefab "stat bar" UI object with a generic script that has a "variable to watch" property, so I can just select the player object and the property in the inspector when I create a new UI component.

This inspector already exists but I can't figure out how to create the inspector element or how I'd even use it (see image attachment).

screen-shot-2017-12-31-at-31758-pm.png (92.2 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by jdean300 · Dec 31, 2017 at 02:38 AM

That is not what that inspector is doing. The inspector in that image is allowing you to select a function to call whenever the value of the slider changes.

You can't really make something generic for what you want to accomplish... Maybe with a ton of work and some complicated code (and probably the use of reflection), but the closest I can think of that is reasonable is to start with an abstract class like the following:

 public abstract class StatBehaviour : MonoBehaviour {
     public abstract int GetStat(string statName);
 }

And then in every behaviour that has stats, inherit from this class instead of MonoBehaviour and override this method. Example:

 public class Player : StatBehaviour {
     public int Health;
     // ... other stats and such
 
     public override int GetStat(string statName) {
         if (statName == "Health") return Health;
         return 0;
     }

     // ... other Player class stuff
 }

Your StatBar script could then have two fields: one for the StatBehaviour to watch and one for the name of the stat to look at.

Note: A good improvement to make to this simple system is to create a Stat enumeration so that you don't use hard-coded strings.

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 ChristianLinnell · Dec 31, 2017 at 02:41 AM 0
Share

Understood, thank you. That inspector does let you set properties as well, so I assumed I could use something similar to get their values.

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

74 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

Related Questions

[Uni2D + Unity Inspector] Unity extremely slow, when a Sprite is selected 4 Answers

Inspector scripting, adding lists, checkboxes and buttons [With Image] 0 Answers

Can I use custom inspector widgets, such as a slider for a floating point property, without writing a custom editor? 3 Answers

no way to understand this 2 Answers

How to clamp EditorGUILayout controls' width ? 6 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