I'm trying to build a 'tower defence' type game, and the game works fine with one turret. But when I have duplicate turrets dotted around the map, the turrets freak out.
Watch My YouTube Video: http://www.youtube.com/watch?v=6xCRZZiu9pU
Basically, what happens is when you select a turret, you have the ability to upgrade the firing range, and change many variables. But when I duplicate the turret, the variables get all mixed up (I'm guessing because they are static, as I need to share them).
And of course, duplicating turrets is important as the user will eventually be able to drag on turrets from a GUI - thus duplicating a prefab (I would imagine).
How can I go about making each turret variables unique to them selves? I really need this folks! Below are my script if you want to get an idea how things are laid out:
ClickObject: (script that picks up the mouse click - attached to parent)
#pragma strict
var clicked : boolean = false;
var hit : RaycastHit;
static var ClickedOn : boolean = false;
function Update() {
if(Input.GetMouseButtonDown(0) &&
collider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit, Mathf.Infinity)) {
clicked = !clicked;
if(tag == "Select") tag = "Unselect";
else tag = "Select";
//Debug.Log("clicked" + (clicked? "" : " off"));
}
ClickedOn = clicked;
}
Control: (variables that controls the turrets state - attached to child)
static var Play = false;
static var Radius : int = 10;
static var ModerniseLevel : int = 1;
static var ModernisePlay : boolean = false;
static var DisplayCamera : boolean = true;
static var TurretClicked : boolean = true;
var TurretCam : Camera;
function Start () {
TurretCam.enabled = true;
}
function Update () {
//TurretClicked = ClickObject.ClickedOn;
collider.radius = Radius;
//TurretClicked = ClickObject.ClickedOn;
print(TurretClicked);
if(ModerniseLevel == 2 && ModernisePlay == true){
animation["SpawnMissile"].speed = 0.5;
animation.wrapMode = WrapMode.Once;
animation.Play("SpawnMissile");
ModernisePlay = false;
}
}
RadiusTexture: (positions sphere around turret according to radius - attached to same child)
var RadiusObject : Transform;
var RadiusSphere : Transform;
private var CurrentSize : float;
private var ScaleX = 3.1; //2
private var ScaleY = 0; //0
private var ScaleZ = 3.4; //2.3
private var ScaleXDiv : float = 0;
private var ScaleYDiv : float = 0;
private var ScaleZDiv : float = 0;
private var ScaleXNew : float = 0;
private var ScaleZNew : float = 0;
private var Clicked = false;
var MouseOverClicked = false;
var Texture = true;
function Update ()
{
if(ClickObject.ClickedOn == true)
{
LeftMenu.TurnOn = true;
if(Texture == true)
{
CurrentSize = collider.radius;
ScaleXDiv = (ScaleX * CurrentSize);
ScaleXNew = ScaleXDiv / 10;
ScaleZDiv = (ScaleZ * CurrentSize);
ScaleZNew = ScaleZDiv / 10;
RadiusObject.transform.localScale = Vector3(ScaleXNew,ScaleY,ScaleZNew);
}
else
{
CurrentSize = collider.radius;
RadiusSphere.transform.localScale = Vector3(CurrentSize * 3,CurrentSize * 3,CurrentSize * 3);
}
//Clicked = true;
}
else if(ClickObject.ClickedOn == false)
{
LeftMenu.TurnOn = false;
RadiusObject.transform.localScale = Vector3(0,0,0);
RadiusSphere.transform.localScale = Vector3(0,0,0);
//Clicked = false;
}
}
LeftMenu: (GUI)
private var myLeft:float = 10;
private var myTop:float = 100;
private var myWidth:float = 150;
private var myHeight:float = 40;
var prefab : GameObject;
var gridX = 5;
var gridY = 5;
var spacing = 2.0;
var isDragging:boolean = false;
var hit : RaycastHit;
var Power = 10;
var Strength = 10;
var TurretCamera : Texture;
static var TurnOn = false;
function OnGUI()
{
//Turret Camera Window
if(TurnOn == true){
GUI.DrawTexture(Rect(Screen.width - 300, Screen.height - 300,260,260), TurretCamera, ScaleMode.StretchToFill, false, 30.0f);
}
if (GUI.RepeatButton(Rect(myLeft, myTop, myWidth, myHeight), "Moving Button" ))
isDragging = true;
if (isDragging){
myLeft = Input.mousePosition.x - myWidth*0.5;
myTop = Screen.height - (Input.mousePosition.y + myHeight*0.5);
}
if(Controller.OrbitCamVar == false){
if (GUI.Button(Rect(10,10,50,50),"3D")){
Controller.OrbitCamVar = true;
}
}
else if(Controller.OrbitCamVar == true){
if (GUI.Button(Rect(10,10,50,50),"2D")){
Controller.OrbitCamVar = false;
}
}
if(TurnOn == true)
{
GUI.BeginGroup (new Rect (2, Screen.height - 300, 170, 300));
GUI.Box(Rect(0,0,170, 300),"Turret Info");
GUI.Label (Rect (10, 30, 200, 30), "Name: Machine Turret");
GUI.Label (Rect (10, 80, 200, 30), "Radius: " +Control.Radius); //Max 40!
if (GUI.Button (Rect (170 - 65, 80, 60, 20), "Upgrade")){
if(Control.Radius < 40){
Control.Radius += 3;
}
else
print("Max Radius Reached!");
}
GUI.Label (Rect (10, 110, 200, 30), "Power: " +Power);
if(GUI.Button (Rect (170 - 65, 110, 60, 20), "Upgrade")){
Power += 5;
}
GUI.Label (Rect (10, 140, 200, 30), "Strength: " +Strength);
if(GUI.Button (Rect (170 - 65, 140, 60, 20), "Upgrade")){
Strength += 5;
}
if(GUI.Button (Rect (170/2 - 130/2, 250, 130, 30), "Modernise")){
Control.ModerniseLevel += 1;
Control.ModernisePlay = true;
}
GUI.EndGroup();
}
}
If you do have an idea of how to help me, please give an example - my scripting it still not very strong. I would appreciate this to whom ever can help!
Ollie