Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Sprites not visible

Discussion in '2D' started by UniPixel, Dec 28, 2014.

  1. UniPixel

    UniPixel

    Joined:
    Dec 23, 2014
    Posts:
    8
    Im creating sprites in script:

    Code (CSharp):
    1.    
    2.     public void add(Vector2 pos, float width, float height) {
    3.         Rect rect = new Rect(pos.x * 100, pos.y * 100, width * 100, height * 100);
    4.         string path = "Assets/Mask_texture.jpg";
    5.         Texture2D texture = (Texture2D)Resources.LoadAssetAtPath(path, typeof(Texture2D));
    6.         Sprite sprite = Sprite.Create(texture, rect, new Vector2(0f, 0f), 100f);
    7.         sprite.name = "Mask sprite";
    8.         GameObject obj = new GameObject("Mask");
    9.         obj.AddComponent<SpriteRenderer>();
    10.         SpriteRenderer renderer = obj.GetComponent<SpriteRenderer>();
    11.         renderer.sprite = sprite;
    12.         renderer.sortingOrder = 5;
    13.         obj.transform.position = pos;
    14.     }
    15.  
    They appearing in Play Mode in Scene View and in Game View. But when i build and run the project, they are not visible. I've deleted all other sprites from the scene to exclude sorting layer problems. But still no effect. Any ideas?

    P.S. renderer.isVisible always false for all objects. Why is that?
     
    Last edited: Dec 28, 2014
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    There's no Resources.LoadAssetAtPath available in a build. There are no individual files in a build; .jpg and so on are never used aside from importing into the editor. (BTW, don't use .jpg, it's lossy.)

    --Eric
     
  3. UniPixel

    UniPixel

    Joined:
    Dec 23, 2014
    Posts:
    8
    how should i create a sprite then?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    The best way would be not to do all this in the first place, but just instantiate prefabs.

    Code (csharp):
    1. public GameObject mySpritePrefab;
    2.  
    3. public void AddSprite (Vector2 pos) {
    4.     var sprite = Instantiate (mySpritePrefab) as GameObject;
    5.     sprite.transform.position = pos;
    6. }
    --Eric
     
    theANMATOR2b likes this.
  5. UniPixel

    UniPixel

    Joined:
    Dec 23, 2014
    Posts:
    8
    Okay, thanks! But where will i get mySpritePrefab object? Sorry, i just starting to learn Unity. Is mySpritePrefab will be some sort of prorotype? Do i need to create instance of it somewhere?
    P.S. I need sprite to have different coordinates of an image every time. In fact i posted a question of what i need exactly here, but no one replied.
     
    Last edited: Dec 28, 2014
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
  7. UniPixel

    UniPixel

    Joined:
    Dec 23, 2014
    Posts:
    8
    I red prefab manual and created prefab in editor. But i still can't understand how can i access my prefab from editor in the script? There is always some "myPrefab" member of the class. But how instantiate itself?
    Code (CSharp):
    1. myPrefab = GameObject.Find("Mask Rect");
    is not working.
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Make a public variable that you use for the reference, as shown in my example. Drag and drop the prefab onto the appropriate slot in the inspector. Don't use Find.

    --Eric
     
    theANMATOR2b likes this.
  9. UniPixel

    UniPixel

    Joined:
    Dec 23, 2014
    Posts:
    8
    Ok, made as you say. So now i have this script:
    Code (CSharp):
    1. public class Mask : MonoBehaviour {
    2.  
    3.     static Mask instance = null;
    4.  
    5.     public static Mask inst {
    6.         get {
    7.             if (instance == null) instance = GameObject.FindObjectOfType<Mask>();
    8.             return instance;
    9.         }
    10.     }
    11.  
    12.     public GameObject spritePrefab;
    13.  
    14.     public void add(Vector2 pos, float width, float height) {
    15.         Sprite sprite = Instantiate(spritePrefab) as Sprite;
    16.         Debug.Log (sprite);
    17.     }
    18. }
    19.  
    and prefab attached to it. Now i need to use it as a singleton, but
    Code (CSharp):
    1. GameObject.FindObjectOfType<Mask>();
    find nothing. How can i create an instance of it?
     
  10. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Is the script attached to an active GameObject in the scene? Also, you can just say "instance = this".

    --Eric
     
  11. UniPixel

    UniPixel

    Joined:
    Dec 23, 2014
    Posts:
    8
    That worked, sprite appears.
    P.S.
    it is static method. Outside Unity in C# i can't use members of instance in static methods. How it works in unity?
    P.S.S. No, do i need to attach it to an existing object? Can't i just "create" one myself in the script?
    P.S.S.S.
    Code (CSharp):
    1.     public void add(Rect rect) {
    2.         var obj = Instantiate (spritePrefab) as GameObject;
    3.         SpriteRenderer renderer = obj.renderer as SpriteRenderer;
    4.         Sprite newSprite = Sprite.Create(renderer.sprite.texture, rect,  new Vector2(0f, 0f), 100f);
    5.         renderer.sprite = newSprite;
    6.     }
    is this code correct?)