Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

ScriptableObject - Image and Sprite

Discussion in 'Scripting' started by catafest, Nov 27, 2018.

  1. catafest

    catafest

    Joined:
    May 3, 2014
    Posts:
    78
    I try to use the ScriptableObject like into this video tutorial.
    I want to use image or sprite.
    Using artworkImage.sprite = oneObjectScriptable.artwork; I cannot add image () ;
    If I change to Sprite from Image
    Code (CSharp):
    1.  public Sprite artworkImage;
    2.     // Use this for initialization
    3.     void Start () {
    4.         artworkImage = oneObjectScriptable.artwork;
    I can add sprite but is not show on game running area or scene area.

    I used Unity 2018.2.17f1 (64-bit). I need a good solution to solve this issue. Thank you.
    My scripts:
    1. create a display for the scriptable object
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class DisplayObjectScriptable : MonoBehaviour {
    5.     // public variable of scriptable object create from menu
    6.     public OneObjectScriptable oneObjectScriptable;
    7.     // the variable artwork
    8.     public Image artworkImage;
    9.     // Use this for initialization
    10.     void Start () {
    11.         artworkImage.sprite = oneObjectScriptable.artwork;
    12.     }
    13. }
    14.  
    2. the scriptable object
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3.  
    4. // Create asset menu into Create area
    5. [CreateAssetMenu(fileName = "NewOneObjectScriptable", menuName = "OneObjectScriptable") ]
    6. public class OneObjectScriptable : ScriptableObject {
    7.  
    8.     // Use this for initialization
    9.     /* public string name;
    10.        if the name is used into the project you can use
    11.        public string object_name; or
    12.        public string new name; */
    13.  
    14.     public string object_name;
    15.     // create a variable by Sprite type
    16.     public Sprite artwork;
    17. }
    18.  
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    Are you putting this object into a Canvas? I don't think a spite can show up outside of a canvas.
     
  3. catafest

    catafest

    Joined:
    May 3, 2014
    Posts:
    78