Variables |
Create variables:
Small story:
if statement |
Car Speed Control-1:
using UnityEngine; public class CarSpeedControl1 : MonoBehaviour { float carSpeed1 = 0.0f; public float accelerationMultiplier = 10.0f; void Update() { // Accelerate with the 'W' key if (Input.GetKey(KeyCode.W)) { carSpeed1 += accelerationMultiplier * Time.deltaTime; Debug.Log("Car Speed: " + carSpeed1); } // Decelerate with the 'S' key if (Input.GetKey(KeyCode.S)) { carSpeed1 -= accelerationMultiplier * Time.deltaTime; Debug.Log("Car Speed: " + carSpeed1); } // Translate the car based on the current speed transform.Translate(Vector3.forward * carSpeed1 * Time.deltaTime); } }
Car Speed Control-2:
using UnityEngine; public class CarSpeedControl2 : MonoBehaviour { float carSpeed2 = 0.0f; public float accelerationMultiplier = 10.0f; public float maxSpeed = 10.0f; void Update() { // Accelerate with the 'W' key if (Input.GetKey(KeyCode.W) && carSpeed2 < maxSpeed) { carSpeed2 += accelerationMultiplier * Time.deltaTime; Debug.Log("Car Speed: " + carSpeed2); } // Decelerate with the 'S' key if (Input.GetKey(KeyCode.S) && carSpeed2 > 0.0f) { carSpeed2 -= accelerationMultiplier * Time.deltaTime; Debug.Log("Car Speed: " + carSpeed2); } // Translate the car based on the current speed transform.Translate(Vector3.forward * carSpeed2 * Time.deltaTime); } }
Car Speed Control-3:
using UnityEngine; public class CarSpeedControl3 : MonoBehaviour { float carSpeed3 = 0.0f; public float accelerationMultiplier = 10.0f; public float maxSpeed = 10.0f; void Update() { // Accelerate with the 'W' key if (Input.GetKey(KeyCode.W) && carSpeed3 < maxSpeed) { carSpeed3 += accelerationMultiplier * Time.deltaTime; Debug.Log("Car Speed: " + carSpeed3); } // Decelerate with the 'S' key if (Input.GetKey(KeyCode.S)) { carSpeed3 -= accelerationMultiplier * Time.deltaTime; Debug.Log("Car Speed: " + carSpeed3); } // Log out car states based on speed LogCarState(); // Translate the car based on the current speed transform.Translate(Vector3.forward * carSpeed3 * Time.deltaTime); } void LogCarState() { if (carSpeed3 == 0.0f) { Debug.Log("Car State: Stop"); } else if (carSpeed3 > 0.0f) { Debug.Log("Car State: Moving Forward"); } else if (carSpeed3 < 0.0f) { Debug.Log("Car State: Moving Reverse"); } // Check for overspeed if (carSpeed3 > maxSpeed) { Debug.Log("Car State: Overspeed"); } } }
switch statement |
Random Color Setter:
using UnityEngine; using UnityEngine.UI; public class RandomColorSetter : MonoBehaviour { // Reference to the car's Material public Material carMaterial; // Default colors Color colorRed = Color.red; Color colorBlue = Color.blue; Color colorGreen = Color.green; Color colorYellow = Color.yellow; // Reference to the UI button public Button colorChangeButton; void Start() { // Add a listener to the button colorChangeButton.onClick.AddListener(SetRandomColor); } // Function to set car color with a random color from default options void SetRandomColor() { int randomValue = UnityEngine.Random.Range(0, 4); switch (randomValue) { case 0: carMaterial.color = colorRed; break; case 1: carMaterial.color = colorBlue; break; case 2: carMaterial.color = colorGreen; break; case 3: carMaterial.color = colorYellow; break; default: break; } Debug.Log("Car Color Set to Random Color"); } }
Array & For Loop |
Create Array By Tags:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CreateArrayByTag : MonoBehaviour { public GameObject[] trees; // Start is called before the first frame update void Start() { trees = GameObject.FindGameObjectsWithTag("Tree"); } // Update is called once per frame void Update() { } }
Change all trees color to yellow:
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ChangeTreesColor : MonoBehaviour { public GameObject[] trees; public Material tree_Mat; Color colorYellow = Color.yellow; // Start is called before the first frame update void Start() { // Find all game objects with the specified tag and store them in the array trees = GameObject.FindGameObjectsWithTag("Tree"); // Iterate through each tree and change its material color to yellow for (int i = 0; i < trees.Length; i++) { // Ensure the tree has a Renderer component Renderer treeRenderer = trees[i].GetComponent<Renderer>(); if (treeRenderer != null) { // Access the material of the tree and set its color to yellow treeRenderer.material.color = colorYellow; } } } }
Functions |
Click game object function:
using UnityEngine; using TMPro; public class ClickGameObject : MonoBehaviour { public TMP_Text cubeNameText; // Update is called once per frame void OnMouseDown() { // Change the color to red GetComponent<Renderer>().material.color = Color.red; // Print the cube name on the screen string cubeName = "Clicked Cube Name: " + gameObject.name; Debug.Log(cubeName); // Display the cube name on the TMP Text element if (cubeNameText != null) { cubeNameText.text = cubeName; } } }
Change color function:
using TMPro; using UnityEngine; public class ChangeColorFunctions : MonoBehaviour { public TMP_Text cubeNameText; public Color customColor = Color.green; // New color variable // Update is called once per frame void OnMouseDown() { // Change the color to red GetComponent<Renderer>().material.color = Color.red; // Print the cube name on the screen string cubeName = "Clicked Cube Name: " + gameObject.name; Debug.Log(cubeName); // Display the cube name on the TMP Text element if (cubeNameText != null) { cubeNameText.text = cubeName; } } // Update is called once per frame void Update() { // Check for space key press if (Input.GetKeyDown(KeyCode.Space)) { // Call the ChangeColor function with the customColor variable ChangeColor(customColor); } } // Function to change the color of the GameObject void ChangeColor(Color newColor) { // Change the color of the GameObject to the specified color GetComponent<Renderer>().material.color = newColor; // Print a message to the console Debug.Log("Color Changed to: " + newColor); } }
Distance Calculator Function: