Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
2
Question by Agent654 · Aug 24, 2016 at 01:51 PM · inspectorvariablesreferences

Is it possible to pick reference variables from one component, in another (different) component via the inspector?

I've been searching on this, and as with most questions I post, there's probably an easy answer here that I'm just not finding or formatting my search to find.


Here's the setup:

  • Component Script A includes:
    • public float myFloat

    • public int myInt

  • Component Script B has:
    • public {variable}

The idea being that, in the inspector I can attach Component Script B to an object, and through the inspector pick the GameObject that has Component Script A, and choose "myFloat" as the variable to reference inside of Component Script B.

Is this even possible?


Here's what I'm trying to do:

I'd like to make a simple script that can be attached to UI text objects to auto-update their displayed text values to match that of the referenced variable value (like the above example) - but I don't want to have to write a lot of small scripts to find and make these references at run time (which I already know how to do).

So - with that intention, perhaps there's a different way to accomplish this?

Per the usual, everyone here is awesome and I appreciate any input/help/guidance that could be provided - thanks!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Extrys · Oct 16, 2019 at 12:50 PM

There is another solution you can use! @Glurth method is ok, but you are coupling your classes in that way.


The way to accomplish that without boilerplate code is... Scriptable Objects!


ScriptableObjects are like monobehaviours, without being GameObjects haha!


You can create an instance of a class inside the assets folder,
and use that instance in your classes as a draggable value in the inspector,
then you will be using the same instance of the shared variable.
Both classes are using the SAME instance that contains the variable.


In fact you can create events for variable changes!


You just need to create a ScriptableObject called "SharedFloat" (for example) it should have a float variable types to share!


 using System;
 using UnityEngine;
 
 [CreateAssetMenu]
 public class SharedFloat : ScriptableObject
 {
     //You need to asign an initial and current values, because
     //scriptable objects doesnt reset their state on application quit
     [SerializeField] float initialValue, currentValue;
 
     //The public property to access to operate with the current value
     public float Value
     {
         get => currentValue;
         set
         {
             //if new value is different to the old one then set that value to the currentValue
             //and execute all functions subscribed to the OnValueChanged event
             if (value != currentValue)
             {
                 currentValue = value;
                 OnValueChanged();
             }
         }
     }
 
     //the event that is called on when current value is changed
     public event Action OnValueChanged;
 
 
     //here is where the current value copy initial value on start
     private void OnEnable() => currentValue = initialValue;
 }


alt text



Then in the Player and HealthUI Scripts you can do this!


 //The Script attached to the player
 public class PlayerTest : MonoBehaviour
 {
     public SharedFloat Health;
 
     //when player gets damage
     public void GetDamage(float damage) => Health.Value -= damage;
 }
 
 
 //The Script attached to the UI
 public class HPBarTest : MonoBehaviour
 {
     public SharedFloat Health;
 
     //Subscribe to OnValueChanged at the begining
     private void Awake() => Health.OnValueChanged += UpdateHPUI;
 
     void UpdateHPUI()
     {
         //Do everything what you want here
     }
 }


Boom! You have now Reactive Shared Variables!


sf.png (46.0 kB)
sf.png (46.0 kB)
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 Glurth · Aug 24, 2016 at 01:58 PM

Uncompiled example (may contain errors, example only):

 public class ScriptA:Monobehavior
 {
    public float Anum;
 }
 public Class ScriptB:Monobehavior
 {
    public ScriptA  Aref;  //this public varaiable, allows you to drag an object with a ScriptA component, onto this field, in the inspetcor.
    public float copyOfANum;
    void Start()
    {
       if(Aref!=null)
          copyOfANum= Aref.Anum;  //after confirming the user HAS indeed provided a reference to a ScriptA component, we can look "into" it, with the "." (dot), to get the number we want.
    }
 }

Comment
Add comment · Show 5 · 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 Agent654 · Aug 24, 2016 at 01:59 PM 0
Share

But would there be a way to do this without making (in your example) "Aref" ScriptA type-specific?

avatar image Glurth Agent654 · Aug 24, 2016 at 02:25 PM 0
Share

not with the existsing inspector, no. There is no way to select particular field, of a component, to be dragged.

That being said you COULD define an INTERFACE that provides a way to access Anum, say AnumInterface, and implement this interface in all your components that contain the float (like ScriptA) . Then you can provide a reference to that AnumInterface to script B. "Interfaces" is a fairly advanced c# concept, similar to, but not exactly like, inheritance. Rather than go into detail about that here, I'll suggest you just google search that stuff. Feel free to past back with questions.

Edit: I have not actually implemented interfaces with he unity inspector yet, myself. So note: these comments are just supposition.

avatar image Agent654 Glurth · Aug 24, 2016 at 02:28 PM 0
Share

@glurth Thanks for the guidance - I'll do some digging and respond if additional questions come up. Thanks!

Show more comments

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

56 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

Related Questions

inspector isnt showing scripts 2 Answers

Variable set twice on the same Update call - Second time doesn't update? 2 Answers

How to expose member variables to the inspector in C#? 1 Answer

Inspector Drop Down for Variables 0 Answers

Is there a way to extend the amount of values for inspector? 0 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