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
1
Question by johan-skold · Apr 28, 2010 at 04:10 PM · staticvalues

Assigning shared values easily

I often find myself using private static variables for sharing information between several instances of the same class (I'm against the general over-use of the whole Manager-idiom for reasons that I won't get into here). For instance, in a game I'm currently working on where the player can select several entities in the scene, I have this (slimmed down for relevancy);

using System.Collections.Generic; using UnityEngine;

public class Selectable : MonoBehaviour { private static List<Selectable> _currentlySelected = new List<Selectable>(); public static IEnumerable<Selectable> currentlySelected { get { return _currentlySelected; } } public void Select() { _currentlySelected.Add(this); } public void Deselect() { _currentlySelected.Remove(this); } public static void DeselectAll() { _currentlySelected.Clear(); } }

In this particular case, all is well since the behaviour itself adds the information to the static variable. But what happens when I want a GUI for selected items, say a health bar? I could solve this by making a new GameObject to the scene with its own GUI-script, but using OnGUI would be much more elegant. Here is my issue however. I obviously want a texture or two for my healthbar, and I obviously want all my Selectable to share the same texture, so how do I assign it?

What would be best in my opinion was if I could make another static variable typed as Texture, and assign it once from the editor. That obviously won't work as the Inspector in the editor does not show static variables. Another solution would be to have a public Texture defined on a per-object basis and in my Awake()-method have something like this:

public void Awake() {
    if (_staticTexture == null && _nonStaticTexture != null) {
        _staticTexture = _nonStaticTexture;
    }
}

That way I would only have to assign the variable once and all objects would share it, but it's neither elegant nor effective having to remember what object had the actual reference.

How would you go about solving an issue like this? I'm open for all kinds of alternatives so even if this gets a couple of answers, do not feel your opinion is irrelevant.

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 johan-skold · Apr 28, 2010 at 04:14 PM 0
Share

Please note: I do not intend to have a healthbar for this particular script as all objects obviously doesn't have health. It was just an example used to explain my problem.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by duck · Apr 28, 2010 at 04:29 PM

I'm finding it a little hard to fully understand what you're trying to achieve here, but as far as I can tell, you could do this by creating a prefab of your Selectable Thing.

You could then have the texture property as a public instance variable on the Selectable script.

Once this is in place, you can assign the texture to the prefab and all instances of the prefab will "inherit" this setting unless they explicitly override it (i.e. if you change an instance's texture in the scene hierarchy). This is different from a static variable of course - changing one Selectable item's texture at runtime won't change any of the others, but it seems to match your requirement in a round-about way.

Hope this helps.

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 johan-skold · Apr 29, 2010 at 11:53 AM 0
Share

This is - as far as I know - as close as I can get with the way Unity works at the moment, even if it's not quite what I want. The downside is that if I have 40 different kinds of selectable objects, this becomes tedious to maintain as it's see$$anonymous$$gly impossible to chain prefabs (eg, having a prefab assigned to a prefab).

avatar image
1

Answer by Ehren · Apr 28, 2010 at 04:40 PM

Here are some other options:

  • Create a prefab for your objects, and assign the texture to the prefab. All instances of the prefab will then share a reference to the texture.
  • Create a central object that contains the reference to the texture. When your objects Awake, have them look up this central object (using FindGameObjectsWithTag) and ask it for the texture. You could even use the central object to hold your collection of selected objects. (If you're shying away from a Manager pattern, this solution may not be to your liking.)
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 johan-skold · Apr 29, 2010 at 12:00 PM 0
Share

As I stated above, the prefab becomes harder to maintain the more selectable objects you add, as I really want prefabs for most of my selectable objects (units in an RTS for example - as this happens to be - could end up getting a bunch of scripts and properties). The central object is sort of what I have now, except inverted. Ins$$anonymous$$d of objects looking up the texture in the central object, the central object injects the texture into a static variable. For now it works, but telling the level designer "copy+paste this object to new levels" is prone to errors.

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

Problem setting up an array with a size determined by a variable 2 Answers

car controls gui 0 Answers

RigidBody Jitterbug Question.( PhysicMaterial.Static) 2 Answers

Storing Static String into new String variable 2 Answers

Why is static typing required for access of public JS function or var in Standard Assets 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