Set sorting layer in unity c#

Sorting layer is a property of SpriteRenderer, it’s used to specify which object is at top, and which object is at bottom.

If you want to change the sorting layer in script, you can use following code:

 

 

Unity compilation error: Expression expected after yield return

Today I’m trying to use coroutine to do the animation for Renderer’s alpha value (fade effect), it’s a lot simpler than modifying the alpha value in Update method.

Using the code from Unity Coroutine documentation will cause following error message:

Expression expected after yield return

Modifying yield return to yield return null will solve this problem. So the correct code should be like:

rigidbody2D.velocity.y not working

I’m really new to Unity and want to add a sprite and move it by press “left” and “right” key.

First I add a RigidBody component to the sprite, then add a Script Component.

Following is the code of script file:

But after I pressed the left key, “left” string is outputted to the console, but the sprite never moved. After a lot of trying, I found it’s because I used “RigidBody” component but not “RigidBody2D” component for the sprite. What a foolish mistake..