DEV Community

Cover image for 20 Useful lines of code that can be used for C# scripting in Unity | pt:2
Rizmy Abdulla ๐ŸŽ–๏ธ
Rizmy Abdulla ๐ŸŽ–๏ธ

Posted on

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

1.To instantiate an object at a specific position and rotation:

GameObject object = Instantiate(prefab, position, rotation);
Enter fullscreen mode Exit fullscreen mode

2.To access the components of a GameObject:

Rigidbody rb = gameObject.GetComponent<Rigidbody>();
Enter fullscreen mode Exit fullscreen mode

3.To move a Rigidbody in the direction it is facing:

rb.velocity = transform.forward * speed;
Enter fullscreen mode Exit fullscreen mode

4.To rotate a Rigidbody towards a target position:

Vector3 direction = target.position - transform.position;
rb.rotation = Quaternion.LookRotation(direction);
Enter fullscreen mode Exit fullscreen mode

5.To apply a force to a Rigidbody in a specific direction:

rb.AddForce(direction * force);
Enter fullscreen mode Exit fullscreen mode

6.To set the position of a Transform:

transform.position = new Vector3(x, y, z);
Enter fullscreen mode Exit fullscreen mode

7.To set the rotation of a Transform:

transform.rotation = Quaternion.Euler(x, y, z);
Enter fullscreen mode Exit fullscreen mode

8.To set the scale of a Transform:

transform.localScale = new Vector3(x, y, z);
Enter fullscreen mode Exit fullscreen mode

9.To find an object in the scene by name:

GameObject object = GameObject.Find("ObjectName");
Enter fullscreen mode Exit fullscreen mode

10.To find an object in the scene by type:

GameObject object = GameObject.FindObjectOfType<Rigidbody>();
Enter fullscreen mode Exit fullscreen mode

11.To destroy a GameObject:

Destroy(gameObject);
Enter fullscreen mode Exit fullscreen mode

12.To destroy a GameObject after a delay:

Destroy(gameObject, delay);
Enter fullscreen mode Exit fullscreen mode

13.To set the value of a public variable in another script:

otherScript.variableName = value;
Enter fullscreen mode Exit fullscreen mode

14.To invoke a method in another script:

otherScript.MethodName();
Enter fullscreen mode Exit fullscreen mode

15.To check if a key or button is pressed:

if (Input.GetKeyDown(KeyCode.Space))
{
    // Perform action
}
Enter fullscreen mode Exit fullscreen mode

16.To get the current frame rate

float frameRate = 1 / Time.deltaTime;
Enter fullscreen mode Exit fullscreen mode

17.To load a scene by name:

SceneManager.LoadScene("SceneName");
Enter fullscreen mode Exit fullscreen mode

18.To add a new entry to a Dictionary:

Dictionary<string, int> dict = new Dictionary<string, int>();
dict.Add("Key", value);
Enter fullscreen mode Exit fullscreen mode

19.To check if an object is in a List:

List<GameObject> list = new List<GameObject>();
if (list.Contains(object))
{
    // Object is in the list
}
Enter fullscreen mode Exit fullscreen mode

20.To sort a List by a property of the objects in the list:

list.Sort((a, b) => a.name.CompareTo(b.name));
Enter fullscreen mode Exit fullscreen mode

Thanks For reading...

Top comments (0)