3
\$\begingroup\$

Suppose a Scriptable Object e.g. NewWeapon.asset with variables such as name, description, price etc.

I've also added a public GameObject weaponObject; so that I can drag and drop the 3D model related to this weapon.

Since Scriptable Objects cannot be added in a scene, how can I add a GameObject in the scene with an attached script that will draw data from the NewWeapon.asset and display the 3DModel already specified and load up the rest of the variables?

My goal is to create a script Item.cs which will be attached to a GameObject in the scene and then based on the current ScriptableObject load its name, stats and 3Dmodel

\$\endgroup\$
2
\$\begingroup\$

ScriptableObjects are not components, so you can't add them with "Add Component". But they can be properties of components.

When you create a variable public ScriptableObject foo (or of a class which extends ScriptableObject) in a MonoBehaviour and then add that MonoBehaviour to the scene, you get a box for it in the inspector to which you can drag-and-drop a ScriptableObject.

Example script:

using UnityEngine;

public class ThingWhichHasAScriptableObject : MonoBehaviour {

    public ScriptableObject assignedAsset;

}

A GameObject with that component in the editor:

ScriptableObject

You can now drag your NewWeapon.asset into the "Assigned Asset" slot and then interact with it from the ThingWhichHasAScriptableObject script.

Regarding displaying a 3d model assigned to the ScriptableObject: Just assigning the ScriptableObject to a GameObject in your scene won't cause it to be rendered. You will have to Instantiate that 3d model at runtime.

\$\endgroup\$
6
  • \$\begingroup\$ Hmm, so if I want to access its name I 'd say foo.name = "blah"; right? About the 3Dmodel, I know that it will not be rendered by simply assigning it. If you read my edited question above you will see that I want a generic block of code that would read which 3dmodel is assigned and then display/add it in the scene. Is this something that can be done or am I in deep waters? \$\endgroup\$ – Valamorde May 15 '16 at 8:00
  • \$\begingroup\$ @Karaflakos Yes, that would work. You would define your Item as a class ItemData : ScriptableObject and declare the foo variable in your component of type ItemData. \$\endgroup\$ – Philipp May 15 '16 at 9:03
  • \$\begingroup\$ So I've done what you said and it works perfectly! Any idea on how the add the 3Dmodel in the scene thing would work? \$\endgroup\$ – Valamorde May 16 '16 at 8:52
  • \$\begingroup\$ @Karaflakos That depends on what your definition of "3Dmodel" is. Is it already a full-fledged GameObject with a MeshFilter and a MeshRenderer with Materials assigned? Or is it just a Mesh? \$\endgroup\$ – Philipp May 16 '16 at 9:27
  • \$\begingroup\$ It's a prefab from an asset I downloaded which is ready to use. It only needs to be added in the scene. \$\endgroup\$ – Valamorde May 16 '16 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.