Hello everybody,
I’m a new Unity scripter. After reading the beginner and intermediate lessons about scripting, after tutorials, I want to create a simple game just to have a little practice.
This game will be a tapping one. You have to click (or to tap on mobile) the “screen” to kill one by one monsters.
I have no graphics (my enemies are just a cube, a sphere, etc…) because I just want to script.
I’ve started with simple things and a little bit I’m adding stuff.
Actually I need an advice because I think that my scripts (two) are too disorganized.
I have two scripts one is for prefab enemis (called enemy.cs) and the other one is for managing these enemies and other things like coins ( that will be needed to improve the player damage), but because I read that a class should only do one thing and only one, well I need this advice from you.
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
public int enemyHealth = 10; // Base enemy life
static int count = 0; // Counter to know how many monster have been created (and which is the difficult level
static float gK = 0.015f; // Const used to raise monster life
static int coin = 10; // How many coins the monster will drop, actually it does not increase.
static ManagingEnemies managingEnemies; // Here will be the reference to the ManagingEnemies script.
void Awake ()
{
managingEnemies = GameObject.FindGameObjectWithTag("GameController").GetComponent<ManagingEnemies>(); // Take reference to the other scripts
}
void Start ()
{
enemyHealth = EnemyLife (); // On start the it will be assigned the enemy life as the function below does.
}
private int EnemyLife()
{
float tempH = enemyHealth * ( 1 + NumberPower((count++ - 1),2) * gK ); // The function which calculate the increasing of enemy's life
return (int)tempH;
}
public void TakeDamage(int damage) //Function which manage the damage took.
{
if (enemyHealth > 0)
enemyHealth -= damage;
else if(enemyHealth <= 0)
Death();
}
private float NumberPower(float number, int index) // exponentiation function. I dunno if there is already one.
{
for (int i = 0; i < index; i++)
number *= number;
return number;
}
private void Death() //When the monster dies, this function gives the coin to the player and destroy the old monster.
{
Destroy (gameObject);
managingEnemies.AddCoin (coin);
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ManagingEnemies : MonoBehaviour {
public GameObject[] enemyObj; // Here there is an array with monster prefab. On each monster prefab there is an enemy.cs script
public Text text; // Text item to count the coins
GameObject enemyInstance;
private int damage = 1; // Actual damage
private int coin = 0; // Starting coin
private Enemy enemy; // Variable to get the reference to the enemy script
void Awake () // On Awake I take the reference to the text item and I update it
{
text = GameObject.Find ("Coin").GetComponent<Text> ();
UpdateCoinText ();
}
private void UpdateCoinText() // Function created to update coin text
{
text.text = "Coin: " + coin;
}
void Update() // Each update if there is no enemy instance a new one will be created, if player tapped call the Tap() function
{
if (enemyInstance == null)
CreateEnemy ();
if (CheckTap ())
Tap ();
}
void CreateEnemy() // As the name says, this function creates a new enemy instance and takes the reference to its script.
{
enemyInstance = Instantiate (enemyObj[Random.Range (0, enemyObj.Length)], new Vector2 (0, 0), Quaternion.identity) as GameObject; // Instantiate a random monster among those referenced.
enemy = enemyInstance.GetComponent <Enemy> ();
}
private bool CheckTap() // Simple...but..maybe useless?
{
if (Input.GetMouseButtonDown (0))
return true;
return false;
}
private void Tap() // If the enemy reference is not null, the enemy takes damage, using enemy class
{
if (enemy != null)
enemy.TakeDamage(damage);
}
public void AddCoin(int amount) // Functions to add or subtract coins.
{
coin += amount;
UpdateCoinText ();
}
public bool SubCoin(int amount)
{
if (amount >= coin) {
coin -= amount;
UpdateCoinText ();
return true;
}
return false;
}
}
Maybe this is a long post…but please, I really want to improve my skills…
Thank you