Video – code – Unity C# Scripting : Complete C# for Unity Game Development


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;


public class LevelLoader : MonoBehaviour {

    public int sceneNumber;

	// Use this for initialization
	void Start () {
        print(" Level Loaded");
	}
	
	// Update is called once per frame
	void Update () {

        if (Input.GetMouseButtonDown(0))
        {
            LoadLevel();

            //RestartLevel();

        }

	}


    void LoadLevel()
    {
        SceneManager.LoadScene("level2");
        //SceneManager.LoadScene(sceneNumber);



    }

    void RestartLevel()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }

}


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraShake : MonoBehaviour {

    Transform camTrans;

    public float shakeTime;
    public float shakeRange;

    Vector3 originalPosition;

	// Use this for initialization
	void Start () {

        camTrans = Camera.main.transform;
        originalPosition = camTrans.position;
	}

    // Update is called once per frame
    void Update() {
        if (Input.GetMouseButtonDown(0))
        {
            StartCoroutine(ShakeCamera());
        }
	}

    IEnumerator ShakeCamera()
    {
        float elapsedTime = 0;

        while (elapsedTime < shakeTime)
        {
            Vector3 pos = originalPosition + Random.insideUnitSphere * shakeRange;

            pos.z = originalPosition.z;

            camTrans.position = pos;

            elapsedTime += Time.deltaTime;

            yield return null;
        }

        camTrans.position = originalPosition;
    }







    //IEnumerator Shake()
    //{
    //    float timeElapsed = 0;
    //    while (timeElapsed < ShakeTime)
    //    {
    //        Vector3 pos = originalPos + Random.insideUnitSphere * shakeRange;
    //        pos.z = camTransform.position.z;
    //        //camTransform.position = Vector3.Lerp(camTransform.position, pos, smoothValue);
    //        camTransform.position = pos;

    //        timeElapsed += Time.deltaTime;
    //        yield return null;
    //    }
    //    camTransform.position = originalPos;
    //}
}


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CoRoutineTest : MonoBehaviour {

    GameObject cube;

	// Use this for initialization
	void Start () {

        //StartCoroutine("Test");

        //Test2();

        StartCoroutine("Test3",6);

        //for (int i = 0; i < 6; i++)
        //{
        //    print("Start()");
        //}

        //StopCoroutine("Test3");

	}


    IEnumerator Test()
    {
        print(" CoRoutine Starts");

        yield return new WaitForSeconds(2f);

        GetComponent<Renderer>().material.color = Color.red;

        yield return new WaitForSeconds(2f);

        GetComponent<Renderer>().material.color = Color.green;

        yield return null;

        print(" CoRoutine Ends");
    }

    void Test2()
    {
        for (int i = 0; i < 6; i++)
        {
            print("Test2()");
        }
    }

    IEnumerator Test3(int times)
    {
        for (int i = 0; i < times; i++)
        {
            print("Test3()");

            yield return null;
        }
    }

}


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RotateWithMouse : MonoBehaviour {

    public float rotationSpeed;


    private void OnMouseDrag()
    {
        float Y = Input.GetAxis("Mouse Y") * rotationSpeed;
        float X = Input.GetAxis("Mouse X") * rotationSpeed;

        transform.Rotate(Vector3.right, Y);
        transform.Rotate(Vector3.up, -X);
    }
}


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class InputTest : MonoBehaviour {

	public GameObject ball;

	public float speed;

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {

//		if (Input.GetKeyDown (KeyCode.Space)) {
//		
//			print (" Space Button Pressed First Time ");
//
//			GetComponent<Renderer> ().material.color = Color.green;
//		}
//
//		if (Input.GetKeyUp (KeyCode.Space)) {
//
//			print (" Space Button Released ");
//
//			GetComponent<Renderer> ().material.color = Color.yellow;
//		}
//
//		if (Input.GetKey (KeyCode.Space)) {
//
//			print (" Space Button Held Down ");
//		}

//		if (Input.GetButtonDown ("Jump")) {
//		
//			print (" Space Button Pressed First Time ");
//
//			GetComponent<Renderer> ().material.color = Color.green;
//		}
	


//		float xInput = Input.GetAxis ("Horizontal") * speed * Time.deltaTime;
//
//		float yInput = Input.GetAxis ("Vertical") * speed * Time.deltaTime;
//
//		Input.GetAxisRaw ("Horizontal");
//
//		print (xInput);
//
//		transform.Translate (xInput , yInput, 0);


//		if (Input.GetButtonDown ("Fire1")) {
//
//			print (" Left Click");
//		
//		}
//

//		if (Input.GetMouseButtonDown (0)) {
//
//			//print (" Left Click ");
//
//			print (Input.mousePosition.x + " " + Input.mousePosition.y);
//
//			Vector3 pos = Input.mousePosition;
//			pos.z = 10f;
//
//			pos = Camera.main.ScreenToWorldPoint (pos);
//
//			Instantiate (ball, pos, Quaternion.identity);
//
//		
//		}
//
//		if (Input.GetMouseButtonDown (1)) {
//
//			print (" Right Click ");
//
//		}
//
//		if (Input.GetMouseButtonDown (2)) {
//
//			print (" Wheel Click ");
//
//		}


	}


	void OnMouseDown(){

		Destroy (gameObject);

	}

}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestScript2 : MonoBehaviour {

	GameObject searchObj;

	GameObject[] enemies;

	// Use this for initialization
	void Start () {

		//searchObj = GameObject.Find ("Cube/Cube2/Enemy");

		//searchObj = GameObject.FindWithTag ("Enemy");

		//enemies = GameObject.FindGameObjectsWithTag ("Enemy");

		//Destroy (searchObj);

//		foreach (GameObject enemy in enemies) {
//		
//			Destroy (enemy);
//		}
//
		//GameObject.Find ("Cube").SetActive (false);

		GameObject.Find ("Cube").GetComponent<Rigidbody> ().useGravity = true;

	}
	
	// Update is called once per frame
	void Update () {
		
	}
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LoopTest2 : MonoBehaviour {

	public GameObject[] objects;

	// Use this for initialization
	void Start () {

		foreach(GameObject o in objects ){

			o.SetActive (true);
			//Destroy (o);
		}


	}
	
}