Ok, I've beat my head against a wall on this enough... As the title says, Resource.Load("icons_3"); is returning null. Ive cut back on as much as I can with the code to figure it out (simplifying it). Heres what I have
icon.sprite = Resources.Load<Sprite>(items[i].item.itemIcon);
Pretty simple. The itemIcon is "icons_3"
This is coming out of a sprite that is set as multiple (a sprite sheet sliced up). I can drag the sprite manually onto the object, but that defeats the purpose.
The sprite sheet is in the Assets/Resources/ directory, so it should load off just the name. Originally it was in the Resources/Sprites folder, but to cut back everything, I moved it up a directory.
So im at a total loss with this one. Is it because its a sprite sheet? Do I need to use another method to load a sprite from a sprite sheet? Thanks for all the help in advance guys.
Did a quick test just to make sure.
void Start()
{
SpriteRenderer sr = gameObject.GetComponent<SpriteRenderer>();
sr.sprite = Resources.Load<Sprite>("Sprites/BlueBlock");
}
The Sprite sits in Assets/Misc/Resources/Sprites/.
Works just fine, so I guess you made an mistake somewhere else.
This must be done:
Your Sprite must be set to texture type Sprite (2D and UI).
This will create a "sub-sprite" in the Sprite (with the Sprite Editor) by default at least if it is a single sprite. The name of this might differ from the main sprite if it is multiple, so make sure you have that right. Make sure that the whole path you give to the Load is correct (case sensitive as fare as I know).
EDIT:
For a sprite that is set to multiple and is sliced you need to use Resources.LoadAll. Those sprites have a name property though which you can use for searching in a loop. Alternatively you can create a Dictionary (with string being the name) or more complex also a sprite atlas.
Related
There are two graphical buttons on the canvas (UI->Button). By clicking on the second one, the following script is executed:
But1 = GameObject.FindGameObjectWithTag("ButtonEgg");
But1.GetComponent<Image>().sprite = Resources.Load<Sprite>("Eggs02");
It should replace the value in the first button in the Image component from the default Eggs01 sprite with Eggs02. But instead it gives a sprite none and a white rectangle.
Both pictures are in the Sprites folder, Sprite (2D and UI) is set in the inspector, and Single is set in Sprite mode.
Tried to duplicate the sprite in the Resources folder.
Even tried to specify the path:
Resources.Load<Sprite>("Sprites/Eggs02");
up: Debug.Log(Resources.Load("Sprites/Eggs02")); issues null
but for sure the sprite is in the folder and even tried to copy the path through the context menu still null (((
I'm not a magician, I'm just learning, so I strongly ask you not to kick and help the young man solve the problem.)
Resources.Load only works with assets placed in and a path relative to a folder called Resources ... So unless your sprite is placed in e.g. Asset/Resources/Sprites/Eggs02 it will obviously return null.
In general you don't want to use Resources at all!
Rather have a
public Sprite sprite;
// or
//[SerializeField] private Sprite sprite;
and drag your sprite into the slot in the Inspector and then simply do
But1.GetComponent<Image>().sprite = sprite;
I'm trying to load all the sprites given the current sprite in the sprite renderer on an object however I am unable to load each individual sprite (2d sprite with multiple 'frames', i.e. a spritesheet) no matter what I try.
I've used
Resources.LoadAll<Sprite>(AssetDatabase.GetAssetPath(spriteRenderer.sprite.GetInstanceID()).Replace(".png", ""));
And even
Resources.LoadAll<Sprite>(Path);
But I can't seem to get it to load the things I want (usually it doesn't work at all but on an older version of Unity it loaded circular loading bars and error signs). I have created the frames/sub-sprites of the sprite.
I've tried changing the path to just using the sprite's name to including the filetype to anything else but nothing works.
I am using Unity 2019.1.0f2 if that matters.
So you want to load a bunch of sprites right?
Have you tried this?
Sprite[] loadedSprites = Resources.LoadAll("Sprites", typeof(Sprite));
If you use this your unity must have a folder named Resources. Basically what it says inside LoadAll at the first param is this: "Resources/Sprites" and then it looks for all Types that are a Sprite inside the Sprite folder.
Note: Make sure all the .png files that are planned to be used are signed as Sprite(2D and UI) this can be done by clicking of the png in unity and right corner it wil say Texture Type.
It ended up being a simple misunderstanding.
My folders were all inside the "Assets" folder while they should have been in the "Assets/Resources" folder. Now I can load the sprite renderer's sprite's sprites using
spriteRenderer.sprite.GetInstanceID()).Replace(".png","").Replace("Assets/Resources/","")
It's a bit long but it works!
Thanks to Louis Ingenthron for the hint!
Hi guys i was wondering how to create a shape adjustment with two objects which specifically could be described as the independent cells, one of which is static, and the second one is dynamic and surrounded by "plasma". The movement of the active object must be controllable by the user (WSAD). Collision of the active object with the static one causes the static object to be swallen, though doesn't change it's position stays in place all the time. As the active object moves, passes the swallen object and troughts it out.
See the image below:
Player character
When it comes close enough to pink enemy it's starting to swallow it (surround by yellow thing)
Pink enemy is completely sourrounded when red circle is in the centre of both.
When it leaves enemy it takes off the yellow thing
I was wondering what is the simplest way to do it. I've been thinking about cloth, physics joints, mesh substraction (is it even possible?), some kind of animation... I don't have much time to do it. Can you show me the simplest way. Which tools and approach should i use? I'm not asking for full code or full solution only for some tips.
Tim Hunter mentioned a wonderful way, most perfect in 3D.
You can use another approach in 2D :
Inside OnCollisionEnter2D try finding hit points using Collision2D.contacts . See this reference .
Create some particle effect there.
Disable the enemy
Now play swallowing animation of the player.
At animation end, enable enemy again.
Maybe calculation is little tricky, still efficient.
I have once again problems with Unity. I can't spear much code due the level of secrecy agreement in the game I'm working on but I ran to this one annoying problem when running my code.
SO, I'm creating GameObjects in runtime when entering a certain view in the game. I have created a so called Reader for our game story which reads text from database, splits it to paragraphs, creates GameObject around that paraghraph text (setting text components etc. to it) and then adds that GameObject and its text to a story panel that shows all the objects as a scrollable view inside masked panel.
Hopefully you can keep up with my explanations :D
Everything works fine all the way to the point that the paragraphs are appearing correctly underneath the parent object (the view panel for the texts) and is shown correctly in the scene view, but... the problem is that, although i have made several checks in update loops, Unity just keeps giving random z position to the GameObjects setting them to
position z = -350
which makes them out of the player view and quite hard to read :D
I have debugged many times the position of the GameObjects in update giving it out z position 0 in every frame. This clearly doesn't mach the value the objects have in editor view during runtime...
Has anyone ran in to this kind of problem?
ps. I have treid to close and open Unity, load things again, nothing works.
oh and one thing to mention too is that to stretch the text to fit the size of text content and parent panel width I have used these as examples:
"Unity UI Tutorial - How to make a scrollable list" by rachetandclank3
Hmm... I somehow managed to fix this problem when I changed this:
this.paragraphObject.transform.localPosition.Set(
this.paragraphObject.transform.position.x,
this.paragraphObject.transform.position.y,0f);
to using this instead:
Vector3 newPosition = new Vector3(
this.paragraphObject.transform.position.x,
this.paragraphObject.transform.position.y, 0f);
this.paragraphObject.transform.localPosition = newPosition;
hopefully this helps others that also struggles with this kind of problem.
Source to the solutions: GameObject position.Set() not working
in Vector3 at z position try to change with gameObject.transform.position.z,
replace gameObject with the object name which position is changed mysteriously.
Im making a japanese educational game and made this sprite sheet. Ive already sliced it in the sprite editor and named all the symbols.
Im instantiating 5 prefabs that need to have 5 different sprites with different
symbols. I thought I was gonna be able to either assign the entire sheet to a game object or just keep adding sprite renderers to the game object, but I was wrong.
How can I achieve this?
You can change the sprite directly from code.
this.GetComponent<SpriteRenderer>().sprite = someSprite;
You can dependency inject the sprites themselves using the editor and a public variable for each; or if you prefer, you can use Resources.Load instead.