Sprite Renderer in Unity2D not functioning - c#

View of the Inspector for the Tile prefab
Tile script
Grid Manager script
I am attempting to generate a grid of squares that form a checkerboard pattern. When line25 of the Grid Manager is removed, it generates a plain white grid perfectly fine, but when I do run init to introduce the checkerboard colors, all of the sprites do not render and the tiles are invisible. The colors are assigned to the tiles when they are generated, but the Sprite Renderer is not drawing them.
Expectation: Running init should assign the colors to the generated tiles, making a checkerboard pattern instead of a plain white grid.
Result: Running init assigns the proper colors to each tile, but the Sprite Renderer doesn't render them anymore, resulting in a blank Scene view and Game View

Your colors have an alpha of zero. In the inspector, at the bottom of the colors, you can see a black line. This fills up with white as it is set to be less transparent. You can click on it and set the alpha, or in the script you can set the .a on the color to 1.

Related

tiled draw mode in Sprite Renderer component

Illustrating Image:
Ok so my goal is to fill up the sky with blue. I used the tiled draw mode in Sprite Renderer component but it doesn't seem to be a good choice. I thought of creating a blue square and then put it on top of the sky but is there any way to do it automatically? ( I found something called border but I couldn't understand what these green lines mean? )
You first have to set up the image to tile correctly. In the inspector for the image you want to tile, open the Sprite Editor, and move the border (green box) to select the SKY portion of your image. For example:
After doing this, your sprite should tile correctly:
Do Note:
If you're filling in a solid color for the sky, use the Sliced mode instead of Tiled to reduce polygon counts. (Tiled mode will make new quads for each repeated section, while Sliced will stretch the solid color in only one quad):
(Left is Tiled, right is Sliced)

Unity2D Gameobjects covered by canvas

I am having a problem with my unity canvas as my background is on top and covering my gameobjects. I have tried putting the canvas sorting order to -10000 and changing the canvas rendering mode to overlay/camera/worldspace, but none of them works..
This is how my game looks like right now:
as you can see the white image which is my background is covering my gameobjects..
Attached are my component values for my canvas:
my canvas
Attached are the values for my camera:
my camera
Attached are the component values for my gameobjects:
my hero gameobject
Have you tried setting your game objects z position to a negative value?
For 2D games in Unity, the more negative your Z value is, the "closer" it is to the camera. If you try setting your knight to something like -1, it should no longer be obscured by your background.
Also, make sure to set the camera for your canvas' render camera and you can set the sorting order back to whatever it was before. Just check the individual pieces attached to your canvas to make sure their Z positions aren't "in front" of the game objects.
The background for your game shouldn't be on the canvas, it should be a gameobject with a sprite renderer and need to be attached with a sprite(background image) with a resolution that will fit most mobile screens (assuming you build it for mobile phone, i personally prefer making it 1028X720)
if you still insist on putting it on the canvas then you could set the canvas render mode to world space and set the sorting layer to something less than default or sort order -1
I know it is old post but #user8502296's answer saved me so I wanted to contribute. I changed the Canvas->RenderMode->Screen Space-Camera then I added Render Canvas->Camera->Main Camera.
Canvas

Unity - paint a texture with alpha to make it dissapear partially

I am trying to achieve this effect, I want to "paint" the image with alpha, like erasing part of the image exactly where the user touches or uses the mouse.
I don't know where to start.
You could put each image on a separate Layer and tell your "eraser brush" to ignore the layer behind the one you want to erase using a LayerMask. When the user clicks on the image, it erases part of the image on the first layer, but leaves the image behind it alone.
Credit to Statement from here:
LayerMask, or any "Mask" in Computer Science, is an integer which uses
its 32 bits to represent different flags. While it is an integer, the
value doesn't mean anything useful numerically. What matters is which
flags are turned on or off.
In the case of LayerMask, each flag (each bit) represents one layer.
The layers are selectable at the top of the inspector for each GameObject, you can add and name layers there, and also choose to view different combinations of them (see pic).
If you want to have 2 textures on asingle plane:
- Make a custom shader with 3 textures (background, foreground, mask)
- Then you can paint into that mask texture with grayscale color and shader fades the 2 textures based on the mask color (example, if color is full white, only foreground is visible)
Or if you want to have 3D object behind a separate texture plane (and you only need to erase the existing texture, not draw the same pattern back)
- Could use any default texture which supports alpha
- Just paint into the MainTexture in the material, painting with color alpha 0 makes the area transparent
- Texture needs to have [x] read/write enabled in the importer settings to allow drawing on it
Some related links:
Basic pixel drawing example from unity docs: http://docs.unity3d.com/ScriptReference/RaycastHit-textureCoord.html
Old demo scene, painting to texture and using dissolve shader : http://unitycoder.com/blog/2011/08/10/painting-holes-with-shader-v2-0/
And search for unity blend 2 textures with mask, unity draw to texture etc. to find more examples & shaders.

Lighting covering up sky

I have a 2D tile-based lighting system which is drawn onto a Render Target.
I am also drawing a background which involves mountains, sun/moon, and clouds.
There is also the unlit game; blocks, character, etc.
Here is the order in which everything is drawn:
1. Background
2. Game World
3. Lighting
The problem is that the LIGHTING is covering up the BACKGROUND when it's dark:
Although it's perfectly fine in the day because the light is full:
You might ask, well, why don't you just blend each block, not drawing the lighting on a RenderTarget?
Because that would prevent me from performing smooth lighting, as seen in the pictures. To produce the smooth lighting, I draw squares of light onto a RenderTarget, perform a Gaussian blur and then draw that RenderTarget.
How about not drawing a square of light in empty spaces?
The light blurs onto any adjacent blocks, and not all objects in my game affected by lighting are square, so they would look like a sprite with a blurry square on top of it.
Light NOT being drawn in empty spaces:
Light being drawn everywhere:
Is there any way to keep the background visible, or something else that would help my predicament?
I'd suggest you do the following:
Render lighting to rendertarget
Render scene to rendertarget (leave the background transparent)
Multiply scene with the lighting rendertarget
Render background to screen
Render scene (with the lighting already applied) to the screen
Because the background is added after applying the lighting, it will not be affected.

XNA remove shader effects

I am new to XNA 4.0 and I'm using a shader for my background texture that creates a glow effects on white objects. When I try to add sprites to the background image it also creates the glow on my sprites, how can I remove my shader on the sprites I add on top of my background?
Use 2 seperate SpriteBatch.Begin() ... SpriteBatch.End() sections.
In the first use the Effect that wraps your shader and draw your background.
In the second draw anything you want without applying the shader.

Categories

Resources