Search Unity

  1. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  2. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  3. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Participate with students all over the world and build projects to teach people. Join now!
    Dismiss Notice
  5. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  6. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  7. Want to see the most recent patch releases? Take a peek at the patch release page.
    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. LurkingNinjaDev

    LurkingNinjaDev

    Joined:
    Jan 20, 2015
    Posts:
    1,700
    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. LurkingNinjaDev

    LurkingNinjaDev

    Joined:
    Jan 20, 2015
    Posts:
    1,700
    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:
    316
    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:
    316
    BrodiMAN likes this.