Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. On February 28th the Feedback website will shut down and be redirected to the Unity forums. See the full post for more information.
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Unity 2018.3 is now released.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    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:
    2,868
    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:
    2,868
    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:
    320
    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:
    320
    BrodiMAN likes this.