I have a GameOjbect dynamically created called “Package”. The component “Image” is created and it works. But I can’t figure out how to access the Source Image to assign an image, starting with a sprite called “A.png”. Images are located in Assets/Images. Basically I want to assign this image to the Source Image of the component “Image”. Any suggestions?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Placement : MonoBehaviour
{
public GameObject BuildLetter;
public Sprite ASprite;
// Use this for initialization
void Start ()
{
BuildLetter = new GameObject("Package");
BuildLetter.transform.position = new Vector3(0, 0, 0);
BuildLetter.AddComponent("Image");
ASprite = BuildLetter.GetComponent<Image>("A.png");// this is broken, how do I connect this?
}
// Update is called once per frame
void Update ()
{
}
}
I’m not TOO sure what you’re trying to accomplish here. What are you attaching this script to exactly? Is the GameObject a prefab? Are you trying to build the GameObject during runtime? We need a bit more information to be able to help you. However, to access the actual “PICTURE” of an componenet the code is…
???.GetComponenet.sprite
1 Like
I have a game object called “Package”, consider it a Prefab created by "public GameObject BuildLetter. This Prefab has an “Image” attached to it that will eventually have a different image for each instance of the Prefab.
To put in another way, I’ll have 26 GameObjects each one with it’s own Image that is created entirely by code. Im stuck on getting the images located in my Assets > Images folder to connect to the GameObject’s Component “Image” that is currently being created with "BuildLetter.AddComponent(typeof(Image));
You’ll need to store each image in a variable, which means you’ll have to use the Resources.Load method (look it up HERE). Once you store it as a variable, you can assign it to the Image component of each GameObject. For example…
private Texture2D imageOne;
public GameObject BuildLetter;
imageOne = Resources.Load(“Images/Image One”);
BuildLetter.GetComponent.sprite = imageOne;
That’s what I tired but for some reason my “GetComponent” is generating an error:
Error 1 ‘UnityEngine.GameObject.GetComponent()’ is a ‘method’, which is not valid in the given context D:\UnityProjects\GeneralOfArmy\Assets\Scripts\Placement.cs 18 21 Assembly-CSharp-vs
This is my code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Placement : MonoBehaviour
{
public GameObject BuildLetter;
public Sprite ASprite;
void Start ()
{
BuildLetter = new GameObject("Package");
BuildLetter.transform.position = new Vector3(0, 0, 0);
BuildLetter.AddComponent(typeof(Image));
ASprite = Resources.Load<Sprite>("Images/ASprite.psd");
BuildLetter.GetComponent<Image>.sprite = ASprite;
}
void Update ()
{
}
}
Sorry…that’s my fault. Line 18 should read…
BuildLetter.GetComponent<Image>().sprite = ASprite;
Thanks DRRosen,
That did get rid of my error. Unfortunately the image is still not displaying in my Package > Image Component. Maybe I have to structure it differently. I even tried changing the image name to just A.png and changed the resource.load location to “Images/A.png” to “Assets/Images/A.png”, but still doesn’t work. I’ll keep at it. Thanks for your help!
No problem. Make sure that under each images Import Settings you have it changed from the default of “Texture” to “Sprite (2D and UI)”
Also, note how Resources.Load works. You have to have a folder inside of your project called “Resources”. Everything you need to access within Resources.Load has to be in that Resources folder.
DRRosen,
It’s been awhile, but I now have time to get back on this project. I tried everything you suggested including creating a Resource folder, but for some reason I can’t get my sprite to display. Below is everything I can provide. As you can see in the first screenshot, the “Package” object is created from the script and has the Image component attached, but it still does not find the image A.png that is located in my Assets > Resources > Images folder. I have no idea what I’m missing, do you know?:
Code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Placement : MonoBehaviour
{
public GameObject buildLetter;
public Sprite aSprite;
void Start ()
{
buildLetter = new GameObject("Package");
buildLetter.transform.position = new Vector3(0, 0, 0);
buildLetter.AddComponent(typeof(Image));
aSprite = Resources.Load<Sprite>("Images/A.png");
buildLetter.GetComponent<Image>().sprite = aSprite;
}
void Update ()
{
}
}
Here is a direct link to the images if they aren’t big enough: