- Home /
Need Help With My GAME!
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
Answer by _Petroz · Nov 27, 2010 at 01:23 AM
Communication between scripts which are connected to the same GameObject is easy. For example you could change ClickObject 'Clicked' to non static and then access it from other scripts like this:
var clickObject : ClickObject; clickObject = gameObject.GetComponent(ClickObject);
if (clickObject.Clicked) { // This turret is clicked }
Okay, but what about if you need to connect to a script that in a different child of that object? And what about connecting to a script that isn't even in the gameobject at all? Thanks For The Reply
Also, I tried just adding this to my Click - and I get an Exceptional Error : Object referenced not set to an instance of an object? I'm guessing it returns Null then? Why...
I'm over the moon - I'm very happy with my game now, its all working how I wanted it to. Thanks Everyone!
Glad to hear it's all working for you. By the way, you're game looks pretty cool, best of luck! :)
Answer by Jay_Adams · Nov 27, 2010 at 01:12 AM
I'm a hack so just throwing out ideas.
first is your turret a prefab?
Second, maybe just have a seperate script for each turret (I suppose you wont have too many).
Answer by Pete Roobol · Nov 27, 2010 at 01:45 AM
Hi, Yup too many static vars, a static var is global so if you set it, it will update the same in all script instances of that script. So loose the static unless it needs to be global.
Use GetComponent(OtherScript) to get other script instance,
http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponent.html
eg if(gameObject.GetComponent(ClickObjectScript).clicked){...
Loose the tower instance statics should sort the chao
Good luck :)
Thank you, what do you mean by "Lose the tower instance statics should sort the chao"?
Your answer
Follow this Question
Related Questions
Specific enemy variable affects all enemies 2 Answers
public private and static variables in js 1 Answer
Prefab's Script affecting all the Prefab 1 Answer
Static, yet Private, Variables? 1 Answer