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
0
Question by casimps1 · Nov 01, 2011 at 05:02 PM · prefabvariableinstancepropertyprivate

Is it possible to set up a prefab variable such that it can only be modified on the prefab and not on the instance?

I'm thinking of situations like health pickups. Let's say you've got 3 prefabs: small, medium, and large health pickups. They each are running a health pickup script with an exposed variable called "healthAmount". The only difference between the 3 prefabs are a different mesh and a different healthAmount value.

Now, if these pickups are sprinkled all over the game, you don't want to have any individual instances overriding the healthAmount b/c you want to maintain consistency and player expectations. However, for game balance tweaking, a designer may want to modify the healthAmount value on the prefab itself.

So, my question is, is this possible? Is there a way to allow healthAmount to be modified on the prefab, but not on the instances? Or is this a case where we just need to rely on the designers to be responsible? The only other option I can think of is storing the values in private script variables which then would require a programmer to tweak.

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

3 Replies

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

Answer by casimps1 · Nov 08, 2011 at 07:28 PM

Thanks for the suggestions. Here's the solution I ended up going with. I made a TweakableData prefab containing only the following script:

 public class TweakableData : MonoBehaviour {
     public int smallHealthSize;
     public int mediumHealthSize;
     public int largeHealthSize;

     void Start( ) {
         Debug.LogError( "TweakableDataManager should NOT be placed in a scene or otherwise instantiated. Modify the variables directly on the TweakableDataManager prefab." );
     }
     
     // Gets the TweakableData referenced from the SceneManager
     public static TweakableData getTweakableData( ) {
         GameObject smObj = GameObject.FindGameObjectWithTag( "SceneManager" );
         if ( !smObj ) {
             Debug.LogError( "No Scene Manager object found in this scene!" );
         }
         SceneManager sm = smObj.GetComponent< SceneManager >( );
         return sm.tweakableDataPrefab.GetComponent< TweakableData >( );
     }
 }

Then, I added a reference to that prefab in the SceneManager prefab (a GameObject that is required to be placed in every scene of the game).

Now, designers can mess with the variables exposed on the TweakableData prefab without ever touching any instanced objects or writing any code. And instanced objects just access the TweakableData variables by calling TweakableData.getTweakableData( ).smallHealthSize for example.

Note also that this script will produce errors if anyone attempts to instantiate the prefab or forgets to put a SceneManager object in the scene.

I think this covers everything I was looking for. Hope this helps someone else.

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
1

Answer by WillTAtl · Nov 01, 2011 at 07:22 PM

it might be possible with a custom editor, but I'm not sure how you would differentiate the original prefab from one of it's instances.

If you make them private members of the pickups, though, you could have them self-set based on the value in a separate object. You'll need some singleton-like behavior to get this to work, here's a very simple example in js:

I created this script called "globals" and attached it to my GameManager class; if you don't have anything like that, you could attach it to the camera or player, anything that exists exactly once in the scene.

 var globalA:float=100;
 
 static var instance:globals;
 
 function Awake () 
 {
    instance=this;
 }
 
 static function GetGlobalA() : float
 {
     return instance.globalA;
 }



then I created a dummy cube and attached this script...

 private var localA:float;
 
 function Awake () 
 {
     localA=globals.GetGlobalA();
 }

this way, you've protected the value from being edited per-instance, while still allowing a designer to edit the values on whatever object you attached the global script to. Has the side-effect of allowing all these related settings to be edited in one place, rather than having to go to each type of health prefab to edit them individually.

If you need the values to be changeable even while running the game in the editor, you could just eliminate localA entirely and just call globals.GetGlobalA() when you need value A, ex, when the health item is picked up. In fact, you should probably do so regardless, for efficiency's sake.

I've used a pseudo-singleton setup in this example just to keep it as simple as possible; search for "singleton" to find more robust examples and info on singletons if you're interested in extending this idea and making the system more robust.

Hope this helps!

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
1

Answer by Fabkins · Nov 01, 2011 at 09:18 PM

Here is a script that I think will do everything you need. It has some enumerated types for the standard values but if the designer needs to overide he can.

enum healthSizeType { small, medium, large}; var healthSize : healthSizeType = healthSizeType.medium; var healthSizeOveride = [10,50,100] ; // Dont touch on pain of death private var health=0;

function Start() { switch(healthSize) { case 0: Debug.Log("small"); health=healthSizeOveride[0];; break; case 1: Debug.Log("medium"); health=healthSizeOveride[1];; break; case 2: Debug.Log("large"); health=healthSizeOveride[2];; break; } Debug.Log(health); }

Enjoy :)

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 Fabkins · Nov 01, 2011 at 10:24 PM 0
Share

Actually this is more obfuscated than protected, but maybe good enough.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Updating a variable on a script in an instanced object 1 Answer

How can I modify a variable on an instance of a prefab after I created it? 2 Answers

How do I assign a prefab reference to a private variable? 1 Answer

GetComponent returns only the last instance 1 Answer

Updating variable on another script. It is not working. 2 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