• 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 NapmasterC · Jul 06, 2011 at 01:49 PM · components

Default Values for Components

I hope this isn't a stupid question, but...

Is there a way to alter the default values of Components? For example: I want to alter to RigidBody component so that it is automatically set to have no Gravity.

If its not possible to change the defaults, is there a way to save the settings in any way? or is it possible to change the values of a component that is placed on multiple objects?

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ZorbaTHut · Oct 04, 2015 at 09:54 PM

You can use the hierarchyWindowChanged callback to detect when something in the hierarchy has been added (which includes components), then change the values on a newly-added component. You have to be careful not to change things that already existed, and you have to be careful not to screw up prefabs, but it's doable.

Because I'm too lazy to not provide example code, example code:

 using UnityEditor;
 using UnityEngine;
 using System;
 
 [InitializeOnLoad]
 public class DefaultSetter {
     static string m_LastScene;
     static Rigidbody[] m_LastSeenRigidbody;
 
     static void Autorun()
     {
         EditorApplication.hierarchyWindowChanged += ExampleCallback;
     }
 
     static void ExampleCallback ()
     {
         if (m_LastScene != EditorApplication.currentScene)
         {
             m_LastSeenRigidbody = Resources.FindObjectsOfTypeAll(typeof(Rigidbody)) as Rigidbody[];
             m_LastScene = EditorApplication.currentScene;
         }
         else
         {
             Rigidbody[] newRigidbody = Resources.FindObjectsOfTypeAll(typeof(Rigidbody)) as Rigidbody[];
             foreach (Rigidbody rigidbody in newRigidbody)
             {
                 if (!Array.Exists(m_LastSeenRigidbody, element => element == rigidbody))
                 {
                     if (PrefabUtility.GetPrefabObject(rigidbody) == null)
                     {
                         Debug.LogFormat("Setting default rigidbody constraints on new rigidbody {0}", rigidbody);
                         rigidbody.constraints = RigidbodyConstraints.FreezePositionZ | RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY;
                     }
                 }
             }
             m_LastSeenRigidbody = newRigidbody;
         }
     }
 }

That's probably imperfect but it seems to work, for now; report any bugs and I'll fix 'em.

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
2

Answer by Eric5h5 · Jul 07, 2011 at 12:39 AM

You can use a script with the Reset function to set up defaults:

 function Reset () {
     rigidbody.useGravity = false;
 }
 
 @script RequireComponent(Rigidbody);

When you add that script to an object that doesn't have a rigidbody, one will be added, and the gravity is turned off at the same time. If an object already has a rigidbody, adding the script will turn gravity off. Also gravity is turned off if you choose the Reset context menu item.

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 GuyTidhar · Jul 06, 2011 at 08:16 PM

You can't do that exactly since the unity components are final and do not let you use inheritance and extend their capabilities.

You can do one of 2 things:

1) Use a prefab

  - Create a game object. 
  - Attach the rigidbody component to it.
  - Create a prefab in the Project window (right click mouse -> Create -> Prefab).
  - Drag the game object you've created on to the prefab you created.
  - Change the settings of the rigidbody on the prefab (not on the game object!).
  - Drag as many instances of this prefab to the hierarchy pane to create game objects that are copy of the prefab.
  - Now each time you change the prefab - the change effects all the instances of it which you have created.
  - Note: each setting you change on a game object which is connected to a prefab (was created from it), will not effect the other objects or the prefab (although in the GameObject menu you can select "Apply changes to prefab" when you have such a game object selected in order to... Apply those settings to the Master prefab.

2) Create a script that requires the component you wish to have defaults for and change the settings of the component you need during run time, like this (give a meaningful name to the script e.g. "RigidBodyExtended")

 @script RequireComponent(Rigidbody)

 // Duplicate any setting you wish to be able to change in run time
 public var useGravity = false;
 
 private var rigidBody : Rigidbody;
 
 function Awake()
 {
     // Grap the reference to the rigidbody
     rigidBody = GetComponent(Rigidbody);
 
     // Now you can use your own setting
     rigidBody.useGravity = useGravity;
 }
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Accesing components on multiple objects in scene 3 Answers

Best way of keeping track of components 1 Answer

Two AudioSource Components on one Gameobject? 1 Answer

Define game object. 0 Answers

Missing Components error 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