- Home /
What am i doing wrong in this code?
Hey guys, here is a script that ive written by following a tutorial, and the function ShowUpgradeGUI gives these errors Assets/Scripts/LevelMaster.js(181,10): BCE0044: expecting (, found 'ShowUpgradeGUI'. Assets/Scripts/LevelMaster.js(181,27): UCE0001: ';' expected. Insert a semicolon at the end. Assets/Scripts/LevelMaster.js(184,28): BCE0044: expecting :, found '='. I dont understand what Ive done wrong i wrote the code in the same fashion as other ones throughout the script but im not sure why it isnt working. If anyone could please help me out it would be appreciated greatly or even explain why it isnt working?
#pragma strict
//Global Variables
static var playerDamage = 0;
//States
var waveActive : boolean = false;
var spawnEnemies : boolean = false;
var upgradePanelOpen : boolean = false;
//Player Variables
var healthCount : int = 10;
var scoreCount : int = 0;
var cashCount : int = 200;
//Define Wave Specific Variables
var waveLevel : int = 0;
var difficultyMultiplier : float = 1;
var waveLength : float = 30.0;
var intermissionTime : float = 5.0;
private var waveEndTime : float = 0;
//Enemy Variables
var enemyPrefabs : GameObject[];
var flyerSpawns : Transform;
private var flyerSpawnPoints : Transform[];
var respawnMinBase : float = 3.0;
var respawnMaxBase : float = 10.0;
private var respawnMin : float = 3.0;
private var respawnMax : float = 10.0;
var respawnInterval : float = 2.5;
var enemyCount : int = 0;
private var lastSpawnTime : float = 0;
//Turrets
var turretCosts : int[];
var onColor : Color;
var offColor : Color;
var allStructures : GameObject[];
var buildBtnGraphics : UISlicedSprite[];
var costTexts : UILabel[];
private var structureIndex : int = 0;
//
// GUI Items
//GUI Variables
var waveText : UILabel;
var healthText : UILabel;
var scoreText : UILabel;
var cashText : UILabel;
var upgradeText : UILabel;
var upgradeBtn : GameObject;
//NGUI items
var buildPanelOpen : boolean = false;
var buildPanelTweener : TweenPosition;
var buildPanelArrowTweener : TweenRotation;
var upgradePanelTweener : TweenPosition;
//
//Placement plane items
var placementPlanesRoot : Transform;
var hoverMat : Material;
var placementLayerMask : LayerMask;
private var originalMat : Material;
private var lastHitObj : GameObject;
//Upgrade vars
private var focusedPlane : PlacementPlane;
private var structureToUpgrade : Turret_Base;
private var upgradeStructure : GameObject;
private var upgradeCost : int;
//
//--
//called first thing on level start
function Start ()
{
//reset the structure index, refresh the GUI
structureIndex = 0;
UpdateGUI();
//Gather all the flyer spawn points into an array, so we dont have to do it manually
flyerSpawnPoints = new Transform[flyerSpawns.childCount];
var i : int = 0;
for(var theSpawnPoint : Transform in flyerSpawns)
{
flyerSpawnPoints[i] = theSpawnPoint;
i ++;
}
//
//
SetNextWave(); //setup the next wave variables (ie difficulty, respawn time, speed etc)
StartNewWave(); //start the new wave
//
}
//
function Update ()
{
//--GUI
if(buildPanelOpen)//if the build panel is open
{
//create a ray, and shoot it from the mouse position, forward into the game
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if(Physics.Raycast (ray, hit, 1000, placementLayerMask))
{
if(lastHitObj)//if we had prevously hit something
{
lastHitObj.renderer.material = originalMat;
}
lastHitObj = hit.collider.gameObject;
originalMat = lastHitObj.renderer.material;
lastHitObj.renderer.material = hoverMat;
}
else//if the raycast didnt hit anything
{
if(lastHitObj)//if we had previously hit something
{
lastHitObj.renderer.material = originalMat;//visually deselect that object
lastHitObj = null;//nullify the plane selection this is so that we cant accidentally drop turrets without a proper and valid location selected
}
}
//drop turrets on click
if(Input.GetMouseButtonDown(0) && lastHitObj && !upgradePanelOpen)
{
focusedPlane = lastHitObj.GetComponent(PlacementPlane);
if(focusedPlane.isOpen && turretCosts[structureIndex] <= cashCount)
{
//drop the chosen structure exactly at the tiles position and rotation of zero
var newStructure : GameObject = Instantiate(allStructures[structureIndex], lastHitObj.transform.position, Quaternion.identity);
//set the new structure to have a random rotation. just for looks
newStructure.transform.localEulerAngles.y = (Random.Range(0,360));
//set the tiles tag to "taken" so we cant double place structures
focusedPlane.myStructure = newStructure;
focusedPlane.isOpen = false;
cashCount -= turretCosts[structureIndex];
UpdateGUI();
}
else if(focusedPlane.myStructure != null)//if the plane already holds a turret
{
ShowUpgradeGUI();
}
}
//--GUI
//Waves
if(waveActive)
{
if(Time.time >= waveEndTime)
{
//Debug.Log("wave over");
//stop spawning enemies
spawnEnemies = false;
//check for remaining enemies
if(enemyCount == 0)
{
FinishWave(); //end this wave
}
}
if(spawnEnemies)
{
if(Time.time > (lastSpawnTime + respawnInterval))//wave is still going. spawn enemies
{
SpawnNewEnemy();
}
}
}
}
//Upgrading Structures
function ShowUpgradeGUI ()
{
//get the planes structure and that structures upgrade options
structureToUpgrade = focusedPlane.myStructure.GetComponent(Turret_Base);
upgradeStructure = structureToUpgrade.myUpgrade;
//if the structure can be upgraded show menu
if(upgradeStructure != null)
{
upgradePanelOpen = true;//first off set the state
upgradeCost = structureToUpgrade.myUpgradeCost;//get the upgrade cost
var upgradeName = structureToUpgrade.myUpgradeName;//get the upgrade name
upgradeText.text = "Upgrade to" + upgradeName + "for $" + upgradeCost + "?";//set the text
CostCheckButton(upgradeBtn, upgradeCost);//set the "confirm" button to active or not based on cost
upgradePanelTweener.Play(true);//fly in the panel
}
}
function ConfirmUpgrade()
{
var spawnPos = structureToUpgrade.transform.position;//get tower pos
var spawnRot = structureToUpgrade.transform.rotation;//get tower rot
Destroy(structureToUpgrade.gameObject);//destroy old tower
var newStructure : GameObject = Instantiate(upgradeStructure, spawnPos, spawnRot);//spawn in new upgraded tower
focusedPlane.myStructure = newStructure;
cashCount -= upgradeCost;//subtract upgrade cost
UpdateGUI();//update the GUI
upgradePanelTweener.Play(false);//hide the upgrade panel
upgradePanelOpen = false;//update the state
}
function CancelUpgrade()
{
upgradePanelTweener.Play(false);//hide the upgrade panel
upgradePanelOpen = false;//update the state
}
//
//end the wave
function FinishWave ()
{
waveActive = false;
//wait for it
yield WaitForSeconds(intermissionTime);
//on to the next
SetNextWave();
StartNewWave();
}
//
//prepare for next wave
function SetNextWave()
{
waveLevel ++;//up the wave level
difficultyMultiplier = ((Mathf.Pow(waveLevel, .5)) * .25);//up the difficulty exponentially
respawnMin = respawnMinBase * (1/difficultyMultiplier);//apply diff mult to respawn times (ie more unity)
respawnMax = respawnMaxBase * (1/difficultyMultiplier);
}
//
//start the new wave
function StartNewWave ()
{
//Set the GUI
UpdateGUI();
//spawn the first enemy
SpawnNewEnemy();
//set the wave end time
waveEndTime = Time.time + waveLength;
//activate the wave
waveActive = true;
spawnEnemies = true;
}
//
/*
function SpawnFlyer()
{
nextFlyerSpawnTime += flyerInterval
var i : int = Random.Range(0,flyerSpawnPoints,length);
var newFlyer : GameObject = Instantiate(flyerPrefab, flyerSpawnPoints[i].position, flyerSpawnPoints[i].rotation);
}
*/
//spawn new enemies
function SpawnNewEnemy()
{
//get a random index to choose an enemy prefab with
var enemyChoice = Random.Range(0,enemyPrefabs.length);
//since flying and ground units probably shouldnt in the same location, we have to seperate this part and spawn based on a tag
var spawnChoice : int;
if(enemyPrefabs[enemyChoice].tag == "FlyingEnemy")
{
//get a random index to choose spawn location with
spawnChoice = Random.Range(0,flyerSpawnPoints.length);
//spawn the flyer at a chosen location and rotation
Instantiate(enemyPrefabs[enemyChoice], flyerSpawnPoints[spawnChoice].position, flyerSpawnPoints[spawnChoice].rotation);
}
else
{
//here we would choose a ground based spawn location
}
//let the game know we just added an enemy for keeping track of wave completion
enemyCount ++;
//set the current time as last spawntime
lastSpawnTime = Time.time;
//re-randomize the respawn interval
respawnInterval = Random.Range(respawnMin, respawnMax);
}
//
//--Custom Functions--//
//this function will eventually contain all generic update events
//this makes sure we dont have the same small parts being called over and over in different ways throughout the script
function UpdateGUI ()
{
//Go through all structure buttons and set them to "off"
for(var theBtnGraphic : UISlicedSprite in buildBtnGraphics)
{
theBtnGraphic.color = offColor;
}
//set the selected build button to "on"
buildBtnGraphics[structureIndex].color = onColor;
waveText.text = "Wave :" + waveLevel;
scoreText.text = "Score :" + scoreCount;
healthText.text = "Health :" + healthCount;
cashText.text = "Cash :" + cashCount;
CheckTurretCosts();
}
//called whenever a structure choice is clicked(the button in the build panel)
function SetBuildChoice (btnObj : GameObject)
{
//when the buttons are clicked they send along their game object as a parameter which is caught by "btnObj" above
//we then use that btnObj variable and get its name so we can easily check exactly which button was pressed
var btnName : String = btnObj.name;
//here we set an "index" based on which button was pressed
//by doing this we can easily tell from anywhere in the script which structure is currently selected
//also if we order things just right we can use this "index" to reference the correct array items automatically
if(btnName == "Btn_Cannon")
{
structureIndex = 0;
}
else if(btnName == "Btn_Missile")
{
structureIndex = 1;
}
else if(btnName == "Btn_Mine")
{
structureIndex = 2;
}
//call this as a seperate function so that as things get more complicated all GUI can be updated at once in the same way
UpdateGUI();
}
//Happens whenever the build panel arrow button is clicked
function ToggleBuildPanel()
{
if(buildPanelOpen)//is the build panel already open
{
//hide all build tiles
for(var thePlane : Transform in placementPlanesRoot)
{
thePlane.gameObject.renderer.enabled = false;
}
//fly out the build panel
buildPanelTweener.Play (false);
//rotate the build panel arrow
buildPanelArrowTweener.Play (false);
//mark the panel as "closed"
buildPanelOpen = false;
}
else //...the build panel was closed, so instead to this...
{
for(var thePlane : Transform in placementPlanesRoot)
{
thePlane.gameObject.renderer.enabled = true;
}
//fly in the build panel
buildPanelTweener.Play (true);
//rotate the build panel arrow
buildPanelArrowTweener.Play (true);
//mark the panel as "open"
buildPanelOpen = true;
}
}
//check to make sure we can purchase
function CheckTurretCosts()
{
//iterate over all structure buttons
for(var i : int = 0;i < allStructures.length; i++)
{
if(turretCosts[i] > cashCount)
{
costTexts[i].color = Color.red; //Set cost text to red color
buildBtnGraphics[i].color = Color(.5,.5,.5,.5); //Set btn graphic to half alpha grey
buildBtnGraphics[i].transform.parent.gameObject.collider.enabled = false; //disable this btn
}
else //we can afford this buttons turret
{
costTexts[i].color = Color.green; //set cost text to green color
if(structureIndex == i) // is this button currently selected for placement
buildBtnGraphics[i].color = onColor; //set the color to "on"
else // this button is not currently selected
buildBtnGraphics[i].color = offColor; //set the color to "off"
buildBtnGraphics[i].transform.parent.gameObject.collider.enabled = true; // enable this button
}
}
}
//Generic function to quickly check if we can afford an item and apply colors and settings to the items button
function CostCheckButton(theBtn : GameObject, itemCost : int)
{
if(cashCount < itemCost)//we cant afford this item
{
theBtn.transform.Find("Label").gameObject.GetComponent(UILabel).color = Color.red;//set costs text to red color
theBtn.transform.Find("Background").gameObject.GetComponent(UISprite).color = Color(.5,.5,.5,.5);//set btn graphic to half alpha grey
theBtn.collider.enabled = false;//disable button collider
}
else//we can afford this item
{
theBtn.transform.Find("Label").gameObject.GetComponent(UILabel).color = Color.green;//set costs text to red color
theBtn.transform.Find("Background").gameObject.GetComponent(UISprite).color = onColor;//set the color to on
theBtn.collider.enabled = true;//disable button collider
}
}
Answer by SinisterRainbow · Jul 01, 2013 at 05:31 AM
You are missing a bracket around line 110 in this code, aka, this code isn't closed:
if(Physics.Raycast (ray, hit, 1000, placementLayerMask))
may be more than that, but that will solve some errors.
np, happens to everyone.. u'll know what the compiler is complaining about after doing this a few times (=
:) By the way have you ever coded with ngui before? Im trying to add a health bar to display enemy health
no, i'm working on my own GL supported gui right now actually. But i'm sure someone can help
Your answer
Follow this Question
Related Questions
Why doesnt my script work 2 Answers
Error... that I don't understand. 3 Answers
Incremental game need help 1 Answer
A node in a childnode? 1 Answer
unity not working help 2 Answers