0% found this document useful (0 votes)
13 views4 pages

44444

1. This document discusses various coding concepts and syntax used in C# scripts in Unity, including: behavior components, variables and functions, if statements, for loops, while loops, do-while loops, foreach loops, scope and access modifiers, and using other classes. 2. It provides code examples to demonstrate each concept, such as changing an object's material color based on key presses, multiplying a variable using a function, testing coffee temperature in an if/else statement, looping through enemies in a for loop, and accessing public and private variables and methods. 3. The document is intended to teach basic C# scripting syntax and structures to someone new to Unity development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

44444

1. This document discusses various coding concepts and syntax used in C# scripts in Unity, including: behavior components, variables and functions, if statements, for loops, while loops, do-while loops, foreach loops, scope and access modifiers, and using other classes. 2. It provides code examples to demonstrate each concept, such as changing an object's material color based on key presses, multiplying a variable using a function, testing coffee temperature in an if/else statement, looping through enemies in a for loop, and accessing public and private variables and methods. 3. The document is intended to teach basic C# scripting syntax and structures to someone new to Unity development.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Scripts as Behaviour Components }


}
3. Conventions and Syntax
using UnityEngine; using UnityEngine;
using System.Collections; using System.Collections;
public class ExampleBehaviourScript : MonoBehaviour public class BasicSyntax : MonoBehaviour
{ {
void Update() void Start ()
{ {
if (Input.GetKeyDown(KeyCode.R)) //this line is there to tell me the x position of my object
{
GetComponent<Renderer> ().material.color = Color.red; /*Hi there!
} * this is two lines!
if (Input.GetKeyDown(KeyCode.G)) * */
{ Debug.Log(transform.position.x);
GetComponent<Renderer>().material.color = Color.green;
} if(transform.position.y <= 5f)
if (Input.GetKeyDown(KeyCode.B)) {
{ Debug.Log ("I'm about to hit the ground!");
GetComponent<Renderer>().material.color = Color.blue; }
} }
} }
}

2. Variables and Functions 4. IF Statements


using UnityEngine;
using System.Collections;
using UnityEngine;
public class VariablesAndFunctions : MonoBehaviour using System.Collections;
{
int myInt = 5; public class IfStatements : MonoBehaviour
{
float coffeeTemperature = 85.0f;
void Start () float hotLimitTemperature = 70.0f;
{ float coldLimitTemperature = 40.0f;
myInt = MultiplyByTwo(myInt);
Debug.Log (myInt);
} void Update ()
{
if(Input.GetKeyDown(KeyCode.Space))
int MultiplyByTwo (int number) TemperatureTest();
{
int result; coffeeTemperature -= Time.deltaTime * 5f;
result = number * 2; }
return result;

void TemperatureTest () using System.Collections;


{
// If the coffee's temperature is greater than the hottest drinking public class WhileLoop : MonoBehaviour
temperature... {
if(coffeeTemperature > hotLimitTemperature) int cupsInTheSink = 4;
{
// ... do this.
print("Coffee is too hot."); void Start ()
} {
// If it isn't, but the coffee temperature is less than the coldest drinking while(cupsInTheSink > 0)
temperature... {
else if(coffeeTemperature < coldLimitTemperature) Debug.Log ("I've washed a cup!");
{ cupsInTheSink--;
// ... do this. }
print("Coffee is too cold."); }
} }
// If it is neither of those then...
else 7. DoWhileLoop
{
// ... do this. using UnityEngine;
print("Coffee is just right."); using System.Collections;
}
} public class DoWhileLoop : MonoBehaviour
} {
void Start()
{
5. ForLoop bool shouldContinue = false;
using UnityEngine; do
using System.Collections; {
print ("Hello World");
public class ForLoop : MonoBehaviour
{ }while(shouldContinue == true);
int numEnemies = 3; }
}
void Start ()
8. ForeachLoop
{
for(int i = 0; i < numEnemies; i++) using UnityEngine;
{ using System.Collections;
Debug.Log("Creating enemy number: " + i);
} public class ForeachLoop : MonoBehaviour
} {
} void Start ()
{
6. WhileLoop string[] strings = new string[3];
using UnityEngine; strings[0] = "First string";
strings[1] = "Second string"; }
strings[2] = "Third string"; }

foreach(string item in strings) 10. AnotherClass


{
print (item); using UnityEngine;
} using System.Collections;
}
} public class AnotherClass
{
public int apples;
9. ScopeAndAccessModifiers public int bananas;
using UnityEngine;
using System.Collections; private int stapler;
private int sellotape;
public class ScopeAndAccessModifiers : MonoBehaviour
{
public int alpha = 5; public void FruitMachine (int a, int b)
{
int answer;
private int beta = 0; answer = a + b;
private int gamma = 5; Debug.Log("Fruit total: " + answer);
}
private AnotherClass myOtherClass;
private void OfficeSort (int a, int b)
{
void Start () int answer;
{ answer = a + b;
alpha = 29; Debug.Log("Office Supplies total: " + answer);
}
myOtherClass = new AnotherClass(); }
myOtherClass.FruitMachine(alpha, myOtherClass.apples);
}
11. 11. Awake and Start

void Example (int pens, int crayons)


{ using UnityEngine;
int answer; using System.Collections;
answer = pens * crayons * alpha;
Debug.Log(answer); public class AwakeAndStart : MonoBehaviour
} {
void Awake ()
{
void Update () Debug.Log("Awake called.");
{ }
Debug.Log("Alpha is set to: " + alpha);

void Update ()
void Start () {
{ if(Input.GetKeyUp(KeyCode.Space))
Debug.Log("Start called."); {
} myLight.enabled = !myLight.enabled;
} }
}
}
12. 12. Update and FixedUpdate

14. ActiveObjects
using UnityEngine;
using UnityEngine; using System.Collections;
using System.Collections;
public class ActiveObjects : MonoBehaviour
public class UpdateAndFixedUpdate : MonoBehaviour {
{ void Start ()
void FixedUpdate () {
{ gameObject.SetActive(false);
Debug.Log("FixedUpdate time :" + Time.deltaTime); }
} }
15. CheckState
using UnityEngine;
void Update () using System.Collections;
{
Debug.Log("Update time :" + Time.deltaTime); public class CheckState : MonoBehaviour
} {
} public GameObject myObject;

13. Enabling and Disabling Components


void Start ()
{
using UnityEngine; Debug.Log("Active Self: " + myObject.activeSelf);
using System.Collections; Debug.Log("Active in Hierarchy" + myObject.activeInHierarchy);
}
public class EnableComponents : MonoBehaviour }
{
private Light myLight; 16. TransformFunctions
using UnityEngine;
using System.Collections;
void Start ()
{ public class TransformFunctions : MonoBehaviour
myLight = GetComponent<Light>(); {
} public float moveSpeed = 10f;
public float turnSpeed = 50f;
public Text boolDisplay2;
void Update () public Text boolDisplay3;
{
if(Input.GetKey(KeyCode.UpArrow)) void Start()
transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime); {
graphic.sprite = standard;
if(Input.GetKey(KeyCode.DownArrow)) }
transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);
void Update()
if(Input.GetKey(KeyCode.LeftArrow)) {
transform.Rotate(Vector3.up, -turnSpeed * Time.deltaTime); bool down = Input.GetKeyDown(KeyCode.Space);
bool held = Input.GetKey(KeyCode.Space);
if(Input.GetKey(KeyCode.RightArrow)) bool up = Input.GetKeyUp(KeyCode.Space);
transform.Rotate(Vector3.up, turnSpeed * Time.deltaTime);
} if(down)
} {
graphic.sprite = downgfx;
}
17. CameraLookAt else if (held)
using UnityEngine; {
using System.Collections; graphic.sprite = heldgfx;
}
public class CameraLookAt : MonoBehaviour else if (up)
{ {
public Transform target; graphic.sprite = upgfx;
}
void Update () else
{ {
transform.LookAt(target); graphic.sprite = standard;
} }
}
boolDisplay1.text = " " + down;
boolDisplay2.text = " " + held;
boolDisplay3.text = " " + held;
18. KeyInput }
using UnityEngine; }
using System.Collections;
using UnityEngine.UI; 19. ButtonInput
using UnityEngine;
public class KeyInput : MonoBehaviour using System.Collections;
{ using UnityEngine.UI;
public Image graphic;
public Sprite standard; public class ButtonInput : MonoBehaviour
public Sprite downgfx; {
public Sprite upgfx; public Image graphic;
public Sprite heldgfx; public Sprite standard;
public Text boolDisplay1;

public Sprite downgfx; {


public Sprite upgfx;
public Sprite heldgfx; private Rigidbody rb;
public Text boolDisplay1;
public Text boolDisplay2; private void Awake()
public Text boolDisplay3; {
rb = GetComponent<Rigidbody>();
void Start() }
{
graphic.sprite = standard; void OnMouseDown ()
} {
rb.AddForce(-transform.forward * 500f);
void Update() rb.useGravity = true;
{ }
bool down = Input.GetButtonDown("Jump"); }
bool held = Input.GetButton("Jump");
bool up = Input.GetButtonUp("Jump"); 21. Singleton – scene manager

if(down) using UnityEngine;


{ using UnityEngine.SceneManagement;
graphic.sprite = downgfx; using UnityEngine.UI;
}
else if (held)
{ public class SceneManagerScript : MonoBehaviour
graphic.sprite = heldgfx; {
} public Text ValueTxt;
else if (up) private void Start()
{ {
graphic.sprite = upgfx; ValueTxt.text = PersistentManagerScript.Instance.Value.ToString();
} }
else public void GoToFirstScene()
{ {
graphic.sprite = standard; SceneManager.LoadScene("first");
} PersistentManagerScript.Instance.Value++;
}
boolDisplay1.text = " " + down; public void GoToSecondScene()
boolDisplay2.text = " " + held; {
boolDisplay3.text = " " + held; SceneManager.LoadScene("second");
} PersistentManagerScript.Instance.Value++;
}
}
20. MouseClick
}
using UnityEngine;
using System.Collections;

public class MouseClick : MonoBehaviour


22. Singleton – Persistent manager }
void B1Click(){
using System.Collections; Debug.Log ("Blue Color");
using System.Collections.Generic; sprite = GetComponent<SpriteRenderer>();
using UnityEngine; sprite.color = Color.blue;
}
public class PersistentManagerScript : MonoBehaviour void B2Click(){
{ Debug.Log ("Red Color");
public static PersistentManagerScript Instance { get; private set; } sprite = GetComponent<SpriteRenderer>();
sprite.color = Color.red;
public int Value; }
void B3Click(){
private void Awake() Debug.Log ("Green Color");
{ sprite = GetComponent<SpriteRenderer>();
if (Instance == null) sprite.color = Color.green;
{ }
Instance = this; // Update is called once per frame
DontDestroyOnLoad(gameObject); void Update()
} {
else }
{ }
Destroy(gameObject);
}
} 24. SceneTransition
}
using UnityEngine;
using UnityEngine.SceneManagement;
23. Button UI
using UnityEngine.UI;
public class SceneTransition : MonoBehaviour
{
using System.Collections; public InputField nameInputField;
using System.Collections.Generic; public void StartGame()
using UnityEngine; {
using UnityEngine.UI; string playerName = nameInputField.text;
public class SetColor : MonoBehaviour if (!string.IsNullOrEmpty(playerName))
{ {
SpriteRenderer sprite; // Store the player name for use in the next scene (you can use
public Color newColor; PlayerPrefs or other methods).
public Button B1,B2,B3; PlayerPrefs.SetString("PlayerName", playerName);
// Start is called before the first frame update // Load the next scene.
void Start() SceneManager.LoadScene("WelcomeScene");
{ }
Button btn1 = B1.GetComponent<Button>(); }
btn1.onClick.AddListener(B1Click); }
Button btn2 = B2.GetComponent<Button>();
btn2.onClick.AddListener(B2Click);
25. Script "WelcomeMessage"
Button btn3 = B3.GetComponent<Button>();
btn3.onClick.AddListener(B3Click); using UnityEngine;

using UnityEngine.UI; 27. Scroll Texture Script


public class WelcomeMessage : MonoBehaviour using System.Collections;
{ using System.Collections.Generic;
public Text welcomeText; using UnityEngine;
void Start() public class SrollTex: monoBehaviour
{ {
string playerName = PlayerPrefs.GetString("PlayerName", "Player"); public float ScrollX = 0.5f;
welcomeText.text = "Welcome, " + playerName + "!"; public float ScrollY = 0.5f;
} void update()
} {
float offsetX = Time.time * __________;
float offsetY = Time.time * __________;
26. BallMovement Script GetComponent<Renderer>().material.mainTextureOffset = new
using UnityEngine; Vector2(___________, _________________);
public class BallMovement : MonoBehaviour }
{ }
public float speed = 5.0f;
void Update()
{
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(horizontalInput, verticalInput, 0) * speed
* Time.deltaTime;
transform.Translate(movement);
}
}

27. BallSizeControl

using UnityEngine;
using UnityEngine.UI;
public class BallSizeControl : MonoBehaviour
{
public Slider sizeSlider;
public float minSize = 1.0f;
public float maxSize = 5.0f;
private void Start()
{
sizeSlider.onValueChanged.AddListener(ChangeBallSize);
}
private void ChangeBallSize(float value)
{
float newSize = Mathf.Lerp(minSize, maxSize, value);
transform.localScale = new Vector3(newSize, newSize, newSize);
}
}

You might also like