Unity - Sprite not properly loaded - c#

I ought to have a normal map of tiles as below:
But when I load the scene, either switch from another in inspector (e.g. click "scene/menu", then click this scene), or transit into this scene in gameplay (SceneManager.loadScene), sprites get invisible randomly.
Each time some of the sprites get invisible, and the next time the invisible ones may not be the same. I suspect it is something with Unity editor, since this happens even when I'm not in playing mode.
In Debug.Log I definitely find those sprites (not null). They just don't show up. I have to reload sprites / reload the scene in Unity inspector, but the load doesn't persist.
Neither sprites, SpriteRenderer nor GameObject is null.
As below is a snapshot when some sprites invisible (not missing):
Just click another scene and switch back:

I just accidentally checked in 3D mode, Unity editor is randomizing with z axis. The sprites are there, they were just hidden behind background. This can be fixed then.

Related

Problems with the BoxCollider2D and Pivot

I make a 2D game in Unity. I have a player with BoxCollider2D and Objects with BoxCollider2D and the player can't go through these Objects like it should but when the player walks from down to the top (topdown PixelRPG) Than he got blocked very early. I will try to show it in the pictures. I tried to change the size of the colliders the layers and more but can't find a solution. ProblemCollision2DChest
Collision2DNPC
Collision2DWindow
NoProblem
NoProblemswithObjects
ProblemAgainfromdowntoTop
From my experience, it looks like you simply need to resize your BoxCollider2D to fit the player.
You can either use the Edit Collider button, and in the Unity viewport you can click and drag to change the size of the collider, or change the Size value in the inspector. Since your collider seems to be too large vertically, change the Y value to be smaller.

Setting a button's alphaHitTestMinimumTreshold results into no call to Button.OnPointerDown

I want to create a button that has a custom shape in Unity.
I created a default UI>Button object and set it's Image to the following sprite:
It doesn't show it on this website, but the circle is the only part of this image that is transparent. It's a .png sprite.
When I set this as the sprite for my button, I could click on the entire rect transform of this sprite to activate the button using my button's OnPointerDown() method and un-hide some other sprites around it:
public override void OnPointerDown(PointerEventData eventData)
{
base.OnPointerDown(eventData);
Debug.Log("Foo");
}
Each time any part of the sprite was clicked, transparent or not, "Foo" would print.
Now I set the button alphaHitTestMinimumThreshold to 0.5:
GetComponent<Button>().image.alphaHitTestMinimumThreshold = .5f;
And as the docs suggest, I disabled atlassing...
...and enabled read/write for the sprite:
Yet now, when I click the button in my scene.. nothing happens. No matter where I click, "Foo" doesn't print anymore.
Then I tried changing the alphaHitTestMinimumThreshold to a lot of different values. Again, the entire button was not clickable anymore. If I don't touch the alphaHitTestMinimumTreshold at all however, my "Foo" prints again.
Why does changing alphaHitTestMinimumTreshold to any value make my OnPointerDown method to never be called?
So after 12 hours of messing around with all sorts of options, I found out that there were 3+ consecutive issues causing this behaviour.
If you ever run into your transparent buttons not working, be sure to try the following steps:
Make sure read/write is enabled in the texture import settings
Try if setting the Sprite Mode Mesh Type to Full Rect helps (sometimes the button's hitbox is off)
Disable atlassing under Project Settings > Editor > Sprite Packer > Mode
..and finally what did the trick for me: if you're working in a prefab that's nested inside another prefab (say your have a prefab for your button and then another prefab for your UI canvas/contents), remove the nested prefab from the parent prefab (aka remove the button prefab from the UI prefab), and just add it back in again. It was that freaking simple for me.
I triple checked whether my nested button prefab differed from the button prefab blueprint, and it didn't. But somehow, removing it from the UI prefab and adding it back in again solved my problem with a large sprinkle of magic.
With transparency buttons, I found that detection improves when the image is a child of the button object. So hierarchy is Canvas>Button>Image.
I know, old thread, but it came up so heres my solution:
It did all of the suggestions, but it didn't work.
HOWEVER,
What finally worked for me, was TO SPECIFY THE RIGHT SIZE. My game Object was elongated, but the sprite had "preserve aspect ratio" ticked. I just didn't bother to size it properly.
So when you have an image, set the size of the gameObject to the right dimensions.

Put animated prefab as image in button

I created an animated prefab and want to use it as image in button. Is this possible? If not what can I do to insert an animated image in button.
I assume you are talking about showing an animated 3D prefab on top of your button.
If you have a 3D prefab and want it to be rendered on top of a button, you need to setup a separate render chain.
In essence you add a camera, that renders your object to a RenderTexture. Then you use this RenderTexture as source for an Image component on top of your button.
The critical part is setting up the camera and prefab, so that it doesn't render the whole scene, and isn't visible from whithin the scene.
There are many ways to do that, but I think the easiest way is to place the prefab and camera far away from the rest so that they are outside the view range of the main camera.
Just make sure the camera sees the prefab (and only the prefab) and has the render texture setup as render target.

Steam VR Scene stops responding on reload in Unity

I have a scene that I'm working on using Steam VR 2.0, and Unity 2018.3.2f1. I have a simple statement in it that reloads the scene
private void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
SceneManager.LoadSceneAsync("Final");
}
}
The issue is: when I reload the scene, it stops responding properly. I am still able to move my head around, and hover over objects. And the objects I hover over get highlighted, but they stay highlighted. I'm not able to pick them up, or interact with them in any meaningful way, and I don't know why this is happening.
I've attached a screenshot of the issue below.
As you can see, multiple objects are highlighted, and the hand mesh is weird:
Solutions I've tried--
Using LoadScene instead of LoadSceneAsync
Using Application.LoadScene instead
Tried to edit the Player script in SteamVR library to not add it to Don't Destroy On Load
Any suggestions?
The issue was arising because the Player prefab in SteamVR 2.0 had Do Not Destroy on Load checked. So, there were multiple players being instanced when I reloaded the scene. I unchecked that box, and now everything is in order.
The checkbox is located inside the [SteamVR] object under the Player prefab:

Unity Particle System appearing behind other sprites

I have a particle system BloodSpill that is Instantiated when the player dies by:
Instantiate(bloodSpillEffect, transform.position, transform.rotation);
However, it will appear behind all other sprites (background, ground etc.) NO MATTER what I do. I have the particle system as a prefab and it's sorting layer is set as the same as the player's both programatically and through the options. It's position on the Z axis is also closer to the camera than other objects.
Actually, if I drag one from the prefabs folder and put it in the scene it will appear in front of other sprites, but when I instantiate it through the line of code above it will not show up.
I don't know and am unable to find information on what the difference is between dragging and dropping it from the folder and using Instantiate().

Categories

Resources