• 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 /
This question was closed Oct 22, 2018 at 02:33 PM by Bunny83 for the following reason:

The question is answered, right answer was accepted

avatar image
3
Question by kromenak · Jun 25, 2012 at 12:16 AM · assetmonobehaviourdefault references

Default Asset References for a Script

I noticed that if you select a Monobehavior script in the project tab, you can see all the Monobehavior's public variables in the inspector. It looks like you can insert default links here which would be insanely useful. However, when I assign an asset here, it does not show up when I actually add the component to a GameObject - it goes back to "None".

The reason I'd like to do this is so I can easily access a shader asset that I want to use in the script. If I don't do it this way, I need to find some prefab to add a reference to the shader or create a prefab for this script, but both seem wasteful or backwards.

Is this supposed to work, or am I doing something wrong? It looks like this should work like Unreal's "defaultproperties" section (which again, would be awesome).

Comment
Add comment · Show 6
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 Eric5h5 · Jun 25, 2012 at 12:29 AM 0
Share

Works fine here (Unity 3.4 and Unity 3.5.2).

avatar image kromenak · Jun 25, 2012 at 01:05 AM 0
Share

Huh, I'll double check and make sure I'm not missing something. Thanks!

avatar image Wolfram · Jun 25, 2012 at 01:15 AM 1
Share

When you say "add the component to a GameObject", do you mean in the Editor, or via Script command? IIRC, the default references are an Editor feature, and won't work when using gameObject.AddComponent().

avatar image kromenak · Jun 25, 2012 at 01:20 AM 0
Share

On closer inspection, this feature appears to work if I add the component to an object in the editor, but if I add the component at runtime, the reference is set to null. Is that intended?

avatar image Bunny83 · Jun 25, 2012 at 01:21 AM 1
Share

Also keep in $$anonymous$$d you can only assign Assets to default references. It's not possible to assign scene objects to them since scene objects belong to a certain scene and aren't available at any time.

So you can assign any kind of assets from the project panel but not from the hierarchy panel.

Btw: the default references doesn't show all punlic variables ;) Only reference-type variables that are of type UnityEngine.Object or a derived class.

Show more comments

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Wolfram · Jun 25, 2012 at 01:34 AM

When creating/adding Components via script, the Inspector default references are ignored, since it is an Editor-only feature.

But there are other workarounds you can use in this case. For example:

  • create a prefab where the object is already constructed and assigned as desired (if that's possible)

  • hardcode and create the required references in the script (e.g., new Material(Shader.Find("MyShaderName")) etc.) and assign them to the Component you just created

  • create all necessary default references as public variables in the script that does the AddComponent(), and then assign these public variables via scripting to the Component after you created it. Then you can assign these references in the Inspector (although still not via the "default references" option).

The last suggestions is probably the most convenient one, but it depends on the situation.

Comment
Add comment · Show 2 · 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 SaiSarat · Oct 21, 2018 at 08:14 PM 0
Share

Sorry I didn't got you, can you help me with giving a wide more explanation comparing with my script ??

In the script In was using the "player" as Transforms object but while executing the script it's not happening

public class CameraFollow : $$anonymous$$onoBehaviour {

 public Transform player;

void Update () {

      transform.position = new Vector3(player.position.x, 
      transform.position.y,
      transform.position.z);

   
     
 }

}

avatar image Wolfram SaiSarat · Oct 22, 2018 at 10:28 AM 1
Share

Please open a new question, ins$$anonymous$$d of reactivating ancient questions with completely unrelated problems, that's not how Unity Answers works.

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

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't find Monobehaviours in Asset Folder when using ObjectPicker 0 Answers

Are default references restricted to MonoBehaviour classes? 1 Answer

Audio Manipulation w/ Unity 1 Answer

Change asset directory? 2 Answers

Asset Store Crashes Unity. Help needed! 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