top of page

This is a reference page for Unity Scripts

Skull Toss

Targets

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

public class Targets : MonoBehaviour
{
    private Rigidbody objectsRb;
    private GameManager gameManager;
    public int objectValue;
    public ParticleSystem explosion;

 --------------------------------------------------------------------------   

 

// Start is called before the first frame update
    void Start()
    {

 

//gets the objects Rigidbody then pushes it up, torques it, and randomizes its position using the various functions)

        

objectsRb = GetComponent<Rigidbody>();
        objectsRb.AddForce(ForceUp(), ForceMode.Impulse);
        objectsRb.AddTorque(RandomSpin(), RandomSpin(), RandomSpin(), ForceMode.Impulse);
        transform.position = NewPos();
   

//stores the GameManager gameobject and its GameManager script in a variable   

 

gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();


    }

----------------------------------------------------------------------------

 

    private void OnMouseDown()
    {

 

//refers to gameManager gameIsActive bool

      

if (gameManager.gameIsActive)
        {
            Destroy(gameObject);
 

 

//refers to gameManger script and calls UpdateScore function, ObjectValue is a variable on the GameObject    

 

gameManager.UpdateScore(objectValue);
            Instantiate(explosion, transform.position, explosion.transform.rotation);
        }
        }

 --------------------------------------------------------------------------  

//GameObject collides with Collider below screen

​

private void OnTriggerEnter(Collider other)
    {
        Destroy(gameObject);
        if (!gameObject.CompareTag("Bad"))
        {
//calls GameOver function which turns on he GameOver text and Reset button

            

gameManager.GameOver();
        }
 -----------------------------------------------------------------------//Various functions called in Start        
    }

    Vector3 ForceUp()
    {
        return Vector3.up * Random.Range(12, 16);
    }

    float RandomSpin()
    {
        return Random.Range(-10, 10);
    }

    Vector3 NewPos()

    {
        return new Vector3(Random.Range(-4, 4), -2);

    }
}
 

Game Manager

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

public class GameManager : MonoBehaviour
{
    public List<GameObject> targets;
    public TextMeshProUGUI textScore;
    public TextMeshProUGUI gameOver;
    public GameObject titleScreen;
    public Button button;
    public bool gameIsActive;
    private int waitTime = 2;
    private int score;

   

 

//creates a random GameObject 0-3 every 2 seconds

​

    IEnumerator WaitForTime()
    {
        while (gameIsActive)
        { yield return new WaitForSeconds(waitTime);
            int index = Random.Range(0, targets.Count);
            Instantiate(targets[index]);
}
}

 ----------------------------------------------------------------------------

//places the new score in the TextMeshProGUI when called in OnMouseDown on Targets script, each "target" has a value

​

 public void UpdateScore(int newScore)
    {
        score += newScore;
        textScore.text = "Score:" + score;

    }

  --------------------------------------------------------------------------

//turns on the GameOver text and the Reset button when called in Targets script ( on collision of target object with collider)

​

public void GameOver()
    {
        gameOver.gameObject.SetActive(true);
        button.gameObject.SetActive(true);
        
        gameIsActive = false;
    }

 

----------------------------------------------------------------------------

//On button push this is called to restart the game

​

  public void StartScene()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }

​

----------------------------------------------------------------------------

​

    public void StartGame(int difficulty)
    {

        gameIsActive = true;
        score = 0;
        waitTime = waitTime /= difficulty;
        StartCoroutine(WaitForTime());
        UpdateScore(0);
        titleScreen.gameObject.SetActive(false);

    }
}
 

Difficulty

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

public class Difficulty : MonoBehaviour
{
    private Button button;
    private GameManager gameManager;
    public int difficulty;


    // Start is called before the first frame update
    void Start()
    {
        button = GetComponent<Button>();
        button.onClick.AddListener(SetDiff);
        gameManager = GameObject.Find("GameManager").GetComponent<GameManager>();
    }

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

    }

    public void SetDiff()

    {
        Debug.Log(button.gameObject.name + "was clicked");
        gameManager.StartGame(difficulty);

    }

 

}
 

bottom of page