Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Force Object to Require Asset

Discussion in 'Scripting' started by BrodiMAN, Oct 12, 2018.

  1. BrodiMAN

    BrodiMAN

    Joined:
    Feb 2, 2016
    Posts:
    81
    Hello all,

    I'm wondering if there is a way, via script, to force an object to require a certain asset in the same way that you can force an object to require a component. Like so:

    [RequireComponent (typeof(BoxCollider2D))]

    Thanks
     
  2. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    3,596
    What asset, why and how? It's not clear what do you want to achieve with this. There are many type of assets and they are used many different ways and places.
    How would it be better than load up the asset from code? (requirecomponent has editor advantage)
     
    BrodiMAN likes this.
  3. BrodiMAN

    BrodiMAN

    Joined:
    Feb 2, 2016
    Posts:
    81
    I'm playing with ScriptableObjects. I've created a ScriptableObject asset that will be used in numerous gameObjects all with their own scripts.

    It's just a hassle to have to go through each object to manually add in the ScriptableObject in the editor. I was wondering (hoping) there was a way to force this behaviour through script.
     
  4. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    3,596
    No, there is nothing like that. The best bet if you straight out your naming convention, and start to create predictable scriptable object names, then you can automatically load up (from code) the corresponding SO.
     
    BrodiMAN likes this.
  5. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    339
    Definitely possible. Have your custom editor assign it when you add the component.
     
  6. BrodiMAN

    BrodiMAN

    Joined:
    Feb 2, 2016
    Posts:
    81
    Custom Editor? What?

    Please explain further.
     
  7. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    339
    BrodiMAN likes this.