Introduction
C# is the main language used to create behaviors in Unity because it is easy to learn, powerful, and works well with Unity's tools.
C# is the main language used to create behaviors in Unity because it is easy to learn, powerful, and works well with Unity's tools.
public class MyBehavior : MonoBehaviour
{
void Start()
{
// Code here runs once when the game starts
}
void Update()
{
// Code here runs every frame
}
}public class PlayerMovement : MonoBehaviour { void Update() { float move = Input.GetAxis("Horizontal"); transform.Translate(move * Time.deltaTime, 0, 0); } }
public class ScoreManager : MonoBehaviour { public int score = 0; public void AddPoint() { score += 1; Debug.Log($"Score: {score}"); } }
This script makes a game object jump when the spacebar is pressed. It uses C# to control physics and print a message.
using UnityEngine; public class SimpleJump : MonoBehaviour { public float jumpForce = 5f; private Rigidbody rb; void Start() { rb = GetComponent<Rigidbody>(); } void Update() { if (Input.GetKeyDown(KeyCode.Space)) { rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse); Debug.Log("Jumped!"); } } }
C# works well with Unity's physics and input systems.
Scripts must be attached to game objects to work in Unity.
Debug.Log helps you see messages in Unity's Console for testing.
C# is the main language for writing game behaviors in Unity.
It is easy to learn and integrates tightly with Unity's features.
Using C# scripts lets you control game actions, physics, and interactions clearly.
using UnityEngine;
public class MoveObject : MonoBehaviour {
void Update() {
transform.position += new Vector3(1, 0, 0) * Time.deltaTime;
Debug.Log(transform.position.x.ToString("F2"));
}
}using UnityEngine;
public class HelloWorld : MonoBehaviour {
void start() {
Debug.Log("Hello");
}
}using UnityEngine;
public class PlayerControl : MonoBehaviour {
void Update() {
// Your code here
}
}Pass the test and this pattern moves unknown