Hey guys, i get a problem with this part of code (I know its this part because when i take it out it works fine)
The problem is that in this GUI, the button doesnt appear untill i hover over it or click on it, im following the tower defense tutorial from cgcookie and am on tutorial 3B (Here is the link):
http://cgcookie.com/unity/2012/08/27/unity-tower-defense-tutorial-part-03b-turret-placement/
Please help. Here is the code:
#pragma strict
//NGUI Items
var buildPanelOpen : boolean = false;
var buildPanelTweener : TweenPosition;
var buildPanelArrowTweener : TweenRotation;
// Placement Plane Items
var placementPlanesRoot : Transform;
var hoverMat : Material;
var placementLayerMask : LayerMask;
private var originalMat : Material;
private var lastHitObj : GameObject;
//Build Selection Items
var onColor : Color;
var offColor : Color;
var allStructures : GameObject[];
var buildBtnGraphics : UISlicedSprite[];
private var structureIndex : int = 0;
function Start()
{
//Refresh the GUI
structureIndex = 0;
UpdateGUI();
}
function Update()
{
if(buildPanelOpen)
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if(Physics.Raycast(ray, hit, 1000, placementLayerMask))
{
if(lastHitObj)//if prev hit object
{
lastHitObj.renderer.material = originalMat; //Visually Deselect that object
}
lastHitObj = hit.collider.gameObject;
originalMat = lastHitObj.renderer.material;
lastHitObj.renderer.material = hoverMat;
}
else
{
if(lastHitObj)
{
lastHitObj.renderer.material = originalMat;
lastHitObj = null;
}
}
if(Input.GetMouseButtonDown(0) lastHitObj)
{
if(lastHitObj.tag == "PlacementPlane_Open")
{
var newStructure : GameObject = Instantiate(allStructures[structureIndex], lastHitObj.transform.position, Quaternion.identity);
newStructure. transform.localEulerAngles.y = (Random.Range(0,360));
lastHitObj.tag = "PlacementPlane_Taken";
}
}
}
}
function UpdateGUI()
{
//Set the buttons to "off"
for(var theBtnGraphic : UISlicedSprite in buildBtnGraphics)
{
theBtnGraphic.color = offColor;
}
//Set the selected build button to "on"
buildBtnGraphics[structureIndex].color = onColor;
}
function SetBuildChoice(btnObj : GameObject)
{
var btnName : String = btnObj.name;
if(btnName == "Btn_Cannon")
{
structureIndex = 0;
}
// if(btnName == "Btn_Missile")
// {
// structureIndex = 0;
// }
// if(btnName == "Btn_Mine")
// {
// structureIndex = 0;
// }
UpdateGUI();
}
//When the build panel arrow is clicked
function ToggleBuildPanel()
{
if(buildPanelOpen)
{
//hide the tiles
for(var thePlane : Transform in placementPlanesRoot)
{
thePlane.gameObject.renderer.enabled = false;
}
//Bring the panel out
buildPanelTweener.Play(false);
//Rotate the arrow
buildPanelArrowTweener.Play(false);
//Mark panel as closed
buildPanelOpen = false;
}
else //the build panel was close, so do this...
{
//show the tiles
for(var thePlane : Transform in placementPlanesRoot)
{
thePlane.gameObject.renderer.enabled = true;
}
//Bring the panel in
buildPanelTweener.Play(true);
//Rotate the arrow
buildPanelArrowTweener.Play(true);
//Mark panel as open
buildPanelOpen = true;
}
}