dev-to-codemaker2015-unity-3d-c-scripting-cheatsheet-for-beginners-34f8_source=p...
dev-to-codemaker2015-unity-3d-c-scripting-cheatsheet-for-beginners-34f8_source=p...
Vishnu Sivan
Follow
LOCATION
Vishnu Sivan Ernakulam, Kerala, India
Posted on Apr 30, 2022 • Edited on Jun 16, 2022
10 EDUCATION
Mar Athanasius College
27 1 1
Kothamangalam
cheatsheet for
Development | Mobile App
Development | AI | AR, VR | Jumber,
Jumbo Games
beginners JOINED
Apr 12, 2022
#unity3d #csharp #cheatsheet
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HelloWorld : MonoBehaviour
{
//Variables
//Functions
//Classes
// Start is called before the first frame update
void Start()
{
Debug.Log("Hello World");
}
// Update is called once per frame
void Update()
{
}
}
1. Variables
It is the name of a memory location which holds values and
references of an object. Like any object orient programming
language, the variable declaration contains the accessibility
level (scope). It can be public, private or protected.
Unity made the object referencing simple by showing the
public variables in the inspector. So, the user can easily
provide values or references to that variable from the
inspector.
2. Classes
Classes are the blueprint of the objects. It is a template that
wraps variables and functions together.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HelloWorld : MonoBehaviour
{
public GameObject gameObject1;
private GameObject gameObject2;
// Start is called before the first frame update
void Start()
{
Display();
}
// Update is called once per frame
void Update()
{
}
void Display()
{
Debug.Log("Hello World");
}
}
3. Functions
Functions are a set of codes executed to accomplish a
specific task. It helps to create reusable codes and
implement modularity in your app. Also, we use functions to
make our codes readable and portable.
4. GameObject Manipulation
GameObject is the core component of a Unity project. All
objects such as light, UI and 3D models are derived from
GameObjects class. This is the parent class for all objects we
are using in a unity scene.
// Create a GameObject
Instantiate(GameObject prefab);
Instantiate(GameObject prefab, Transform parent);
Instantiate(GameObject prefab, Vector3 position, Quatern
Instantiate(bullet);
Instantiate(bullet, bulletSpawn.transform);
Instantiate(bullet, Vector3.zero, Quaternion.identity);
Instantiate(bullet, new Vector3(0, 0, 10), bullet.transf
// Destroy a GameObject
Destroy(gameObject);
// Finding GameObjects
GameObject myObj = GameObject.Find("NAME IN HIERARCHY");
GameObject myObj = GameObject.FindWithTag("TAG");
// Accessing Components
Example myComponent = GetComponent<Example>();
AudioSource audioSource = GetComponent<AudioSource>();
Rigidbody rgbd = GetComponent<Rigidbody>();
5. Input System
The input system is the crucial component in every game
that we have played. It might be a keyboard, joystick, or
touch. Unity has a Conventional Game Input to access input
in your games.
if (Input.GetKeyDown(KeyCode.Space)) {
Debug.Log("Space key was Pressed");
}
if (Input.GetKeyUp(KeyCode.W)) {
Debug.Log("W key was Released");
}
if (Input.GetKey(KeyCode.UpArrow)) {
Debug.Log("Up Arrow key is being held down");
}
/* Button Input located under Edit >> Project Settings >
if (Input.GetButtonDown("ButtonName")) {
Debug.Log("Button was pressed");
}
if (Input.GetButtonUp("ButtonName")) {
Debug.Log("Button was released");
}
if (Input.GetButton("ButtonName")) {
Debug.Log("Button is being held down");
}
6. Vector
Vector is a mathematical concept that holds both magnitude
and direction. It is useful to describe some properties such as
the position and velocity of a moving object in your game,
or the distance between two objects. Unity implements
Vector2 and Vector3 classes for working with 2D and 3D
vectors.
Vector3.right /* (1, 0, 0) */
Vector2.right /* (1, 0) */
Vector3.left /* (-1, 0, 0) */
Vector2.left /* (-1, 0) */
Vector3.up /* (0, 1, 0) */
Vector2.up /* (0, 1) */
Vector3.down /* (0, -1, 0) */
Vector2.down /* (0, -1) */
Vector3.forward /* (0, 0, 1) */
Vector3.back /* (0, 0, -1) */
Vector3.zero /* (0, 0, 0) */
Vector2.zero /* (0, 0) */
Vector3.one /* (1, 1, 1) */
Vector2.one /* (1, 1) */
float length = myVector.magnitude /* Length of this Vect
myVector.normalized /* Keeps direction, but reduces leng
7. Time
Unity supports time-related operations through its Time
library. Time.time, Time.deltaTime and Time.timeScale are
the most common APIs to work with time in your project.
8. Physics Events
Unity has a sophisticated system to implement physics in
your project. It various physics attributes to the gameObjects
such as gravity, acceleration, collision and other forces.
// For 2D Colliders
private void OnCollisionEnter2D(Collision2D hit) { }
private void OnCollisionStay2D(Collision2D hit) { }
private void OnCollisionExit2D(Collision2D hit) { }
private void OnTriggerEnter2D(Collider2D hit) { }
private void OnTriggerStay2D(Collider2D hit) { }
private void OnTriggerExit2D(Collider2D hit) { }
9. Rendering materials
Materials tell how a surface should be rendered in the scene.
The material contains references to shaders, textures, color,
emission and more. Every material requires a shader for
rendering the content and the attributes available for that
may vary on different shader values.
10. Lighting
Lighting is a mandatory component in any Unity scene. All
unity scenes contain a directional light component in it by
default. Unity has four types of lights - directional, points,
spot and area lights
11. Coroutine
A coroutine is like a background activity which can hold the
execution of codes after the yield statement until it returns a
value.
// Create a Coroutine
private IEnumerator CountSeconds(int count = 10)
{
for (int i = 0; i <= count; i++) {
Debug.Log(i + " second(s) have passed");
yield return new WaitForSeconds(1.0f);
}
}
// Call a Coroutine
StartCoroutine(CountSeconds());
StartCoroutine(CountSeconds(10));
12. Animation
It is an easy task to create animations in Unity. Unity made it
simple with the help of Animator controls and Animation
Graph. Unity calls the animator controllers to handle which
animations to play and when to play them. The animation
component is used to playback animations.
13. Hotkeys
Image credits patreon.com
Qodo PROMOTED
Read More
Amazing post, you cover a bit more then just the basics,
co-routines are a bit advanced and that really adds to the
quality of this post.
2 likes Reply
Thank you!
1 like Reply
Neon PROMOTED
Read next
Sample Title
Diwakar kothari - Mar 8