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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Add to C# array in Unity not finding or adding sprites

Discussion in 'Editor & General Support' started by arsonist_, Feb 8, 2020.

  1. arsonist_

    arsonist_

    Joined:
    Feb 5, 2020
    Posts:
    2
    I am follow this tutorial
    where he uses a canvas to display the heart sprites.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class Health : MonoBehaviour
    7. {
    8.     public int health;
    9.     public int num_shields;
    10.  
    11.     public Image[] shields;
    12.     public Sprite full_shield;
    13.     public Sprite empty_shield;
    14.  
    15. }
    16.  
    In the unity editor, the script component wont let me drag and drop the sprites into the array as elements. It wont let me find any sprites through search image with the knob beside the array element.
    I don'd know what to do because i can't find any other good tutorials for using heart sprite based health meters

    I have also tried using [SerializeField] in this line
    public Image[] shields;
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    It might be confusion around the difference between a "Sprite" and a "Texture". For example, the following three kinds of properties contain completely different kinds of assets/components:
    Code (CSharp):
    1. public Sprite[] Sprites;
    2. public Texture2D[] Textures;
    3. public Image[] Images;
    Textures are the most general. Most of the "images" in your project are probably textures, and should be assignable to Texture2D.

    "Sprites" are specialized textures, which have been flagged as "Sprites" in the Inspector when selecting a texture. For example:

    upload_2020-2-8_1-47-8.png

    Only if you've set the Texture Type to Sprite will you see that image show up under a Sprite property. That's probably why you're not seeing your textures available for the "Sprite" properties.

    The last, "Image", probably isn't what you want. Instead of being an image asset in your project, that refers to image components, meaning a specific image component you've added to a UI canvas.
     
    samhain323 and arsonist_ like this.
  3. arsonist_

    arsonist_

    Joined:
    Feb 5, 2020
    Posts:
    2
    When I change the array type, so "Sprite[]" or "Texture2D[]" I still can't drag the game objects into the array.

    Edit: I changed the array to be:

    public GameObject[] shields;


    and I was able to drag and drop the game objects into the array
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,120
    All of these things are different types of things, and each allows different sorts of things to be assigned to them. You'll need to understand what's different about these different types, and how that impacts working in Unity. It sounds like you're just starting out with Unity, so I'd recommend watching some very simple tutorials to get up to speed on the naming conventions in Unity. There's a big difference between a Texture2D and a GameObject, for example. You need to understand that difference.