DEV Community

Cover image for 20 Useful lines of code that can be used for C# scripting in Unity | pt:1
Rizmy Abdulla 🎖️
Rizmy Abdulla 🎖️

Posted on • Edited on

20 Useful lines of code that can be used for C# scripting in Unity | pt:1

Here are 20 useful lines of code for C# scripting in Unity:

1.transform.position - This line of code allows you to access and modify the position of a game object in the scene.

2.Instantiate(gameObject) - This line of code allows you to instantiate (create) a new instance of a game object at runtime.

3.Destroy(gameObject) - This line of code allows you to destroy a game object from the scene.

4.GetComponent<T>() - This line of code allows you to access a component of a specific type (T) attached to a game object.

5.Input.GetKeyDown(KeyCode.Key) - This line of code allows you to check if a specific keyboard key (Key) has been pressed down.

6.Random.Range(min, max) - This line of code generates a random number between the minimum (min) and maximum (max) values specified.

7.Time.deltaTime - This line of code returns the time in seconds since the last frame was rendered.

8.Mathf.Clamp(value, min, max) - This line of code clamps (restricts) a value (value) to be between the minimum (min) and maximum (max) values specified.

9.Vector3.Lerp(a, b, t) - This line of code performs a linear interpolation (lerp) between two 3D vectors (a and b) by a factor (t).

10.Quaternion.Euler(x, y, z) - This line of code creates a new quaternion representing a rotation around the x, y, and z axes.

11.rigidbody.velocity - This line of code allows you to access and modify the velocity of a rigidbody attached to a game object.

12.collider.isTrigger - This line of code allows you to enable or disable the trigger behavior of a collider attached to a game object.

13.AudioSource.PlayOneShot(clip) - This line of code plays a one-time audio clip (clip) using an AudioSource attached to a game object.

14.Application.LoadLevel(name) - This line of code allows you to load a new level (name) from the build settings.

15.Screen.width and Screen.height - These lines of code allow you to access the current width and height of the game screen.

16.GUILayout.Label(text) - This line of code displays a text label (text) using the GUILayout system.

17.PlayerPrefs.SetInt(key, value) - This line of code allows you to save an integer value (value) using a key (key) in the PlayerPrefs storage.

18.Physics.Raycast(origin, direction) - This line of code performs a raycast in the specified direction (direction) from the origin (origin) and returns the first hit.

19.Debug.Log(message) - This line of code outputs a message (message) to the console for debugging purposes.

20.coroutine = StartCoroutine(method) - This line of code starts a new coroutine (method) and stores the reference in a variable (coroutine) for later use.

Thanks for reading...

Top comments (0)