C# · Chapter 3.5 · Intermediate level
Methods (Player Actions: Jump() & Attack()!)
मेथड्स (हीरो के एक्शन्स: Jump() और Attack()!)
In a real game, every time you tap the Attack or Jump button on your controller, Unity runs a C# Method like Attack() or Jump()! A Method is C#'s word for a reusable function:
• static void Attack() { ... } teaches the Hero how to attack.
• Writing Attack(); inside Main() triggers the sword slash on screen!
असली गेम में जब आप स्क्रीन पर Jump या Attack बटन दबाते हो, तो पीछे C# का Method (मेथड) चलता है! इसे दो आसान हिस्सों में समझो:
• static void Attack() { ... } लिखकर हम हीरो को सिखाते हैं कि हमला कैसे करना है।
• Main() के अंदर Attack(); लिखकर हम हीरो को हुक्म देते हैं कि अभी तलवार चलाओ!
Words and symbols in this chapter
static void Attack()- Defines a reusable action method named
Attack(void= it returns nothing, it just performs the action). Attackनाम का एक्शन मेथड बनाता है (voidका मतलब यह कुछ लौटाता नहीं, सिर्फ़ काम करता है)।Attack();- Calls
Attack— the hero swings his sword and the enemy loses 20 HP! Attackमेथड चलाता है — हीरो तलवार घुमाएगा और दुश्मन की 20 HP घटेगी!static void Jump() { ... }- A second method we write ourselves — C# has no built-in
Jump(), so every action must be defined in the class. - दूसरा मेथड जो हम ख़ुद लिखते हैं — C# में
Jump()पहले से नहीं होता, इसलिए हर एक्शन class में बनाना पड़ता है। class Program { ... }- All methods, including
Main(), live inside the braces of the class. - सारे मेथड,
Main()समेत, class के braces के अंदर रहते हैं।
Ready example
using System;
class Program {
static void Jump() {
Console.WriteLine("Hero jumps over the pit!");
}
static void Attack() {
Console.WriteLine("Sword Slash! -20 HP");
}
static void Main() {
Jump();
Attack();
Attack();
}
}
What you will see: Watch the Hero jump, swing a glowing sword slash (⚡⚔️💥), and drain the Enemy's HP Bar!
प्रिव्यू में हीरो उछलेगा, तलवार चलाएगा (⚡⚔️💥) और सामने दुश्मन की Health Bar घट जाएगी!
Line by line
static void Jump() { ... }- Our own
Jump()method — every method you call must be defined inside the class. - हमारा अपना
Jump()मेथड — जो भी मेथड बुलाओ, वह class के अंदर बना होना चाहिए। static void Attack() { ... }- Creates the
Attack()method that prints "Sword Slash! -20 HP". Attack()मेथड बनाया जो "Sword Slash! -20 HP" छापता है।static void Main() {- The game starts here!
- गेम यहाँ से शुरू होता है!
Jump(); / Attack();- Calls the methods: the hero jumps, then slashes (twice in this demo).
- मेथड बुलाता है: हीरो उछलता है, फिर तलवार चलाता है (इस डेमो में दो बार)।
Common mistake
✗ Wrong
static void Jump {
...
}✓ Right
static void Jump() {
...
}Method definitions and method calls ALWAYS require parentheses () even when taking zero inputs!
मेथड बनाते और चलाते समय नाम के आगे () लगाना ज़रूरी है — जैसे Jump();!
Your task
Inside class Program, define static void Attack() that prints "Sword Slash! -20 HP", then call Attack(); in Main() after Jump();.
class Program के अंदर static void Attack() बनाओ जो "Sword Slash! -20 HP" छापे, फिर Main() में Jump(); के बाद Attack(); बुलाओ।
- Below
Jump()(still insideclass Program) writestatic void Attack() { Console.WriteLine("Sword Slash! -20 HP"); }.Jump()के नीचे (class Programके अंदर ही)static void Attack() { Console.WriteLine("Sword Slash! -20 HP"); }लिखो। - Inside
static void Main(), afterJump();, callAttack();.static void Main()के अंदरJump();के बादAttack();लिखो। - Click ▶ Run & Preview — the hero jumps and slashes!
▶ Run & Preview दबाओ — हीरो उछलेगा और तलवार चलाएगा!
Starter code for the task
using System;
class Program {
static void Jump() {
Console.WriteLine("Hero jumps over the pit!");
}
// TODO 1: yahan static void Attack() method banao
// jo Console.WriteLine("Sword Slash! -20 HP"); kare
static void Main() {
Jump();
// TODO 2: yahan Attack(); call karo
}
}Do this task in the classroom: your code is checked automatically and you get hints if something is missing.
Recap
- A method is a named, reusable action:
static void Attack() { ... }. - Write every method inside
class Program, next tostatic void Main()— C# has no built-inJump()orAttack(). - Call a method with its name and
():Attack();—voidmeans it returns nothing.
- Method एक नाम वाला, दोबारा चलने वाला काम है:
static void Attack() { ... }। - हर method
class Programके अंदर,static void Main()के साथ लिखो — C# मेंJump()याAttack()पहले से नहीं होते। - Method को नाम और
()से बुलाओ:Attack();—voidका मतलब है यह कुछ नहीं लौटाता।
Quick quiz
What does void mean in static void Jump()?
static void Jump() में void शब्द का क्या मतलब है?
Show the answer
Answer: B (Method performs an action without returning a value)
void means the method performs its action and returns nothing.
void का मतलब है मेथड अपना काम करेगा लेकिन कोई संख्या वापस return नहीं करेगा।
Practice problems for this chapter
- Combo Buttons (static void methods)
कॉम्बो बटन (static void methods)
Solve them in the Practice Zone, where each answer is checked automatically and two hints are available.