Hi guys. I’m making Tower Defence game and i stuck in scrip which i translated from java to c#. i dont know how to do for loops could you help me? (and also i use ngui free)
using UnityEngine;
using System.Collections;
public class INGAme : MonoBehaviour {
//GUI
public bool buildPanelOpen =false;
public TweenPosition BuildPanelTweener;
public TweenRotation BuildPanelArrowTweener;
//Placement
public Transform placementPlanesRoot;
public Material hoverMat;
public LayerMask placementLayrerMask;
private Material originalMat;
public GameObject lastHitObj;
// build
public Color onColor;
public Color offColor;
public GameObject[] allStructures;
public UISlicedSprite[] buildBtnGraphics;
private int structureIndex = 0;
void Start()
{
structureIndex = 0;
ApdateGUI();
}
void ToggleBuildPanel() {
if(buildPanelOpen)
{
//ray
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast(ray, hit, 1000, placementLayerMask));
{
if(lastHitObj)
{
lastHitObj.renderer.material = originalMat;
}
lastHitObj = hit.collider.gameObject;
originalMat = lastHitObj.renderer.material;
lastHitObj.renderer.material = hoverMat;
}
BuildPanelTweener.Play(false);
BuildPanelArrowTweener.Play(false);
buildPanelOpen = false;
}
else
{
if(lastHitObj);
{
lastHitObj.renderer.material = originalMat;
lastHitObj = null;
}
BuildPanelTweener.Play(true);
BuildPanelArrowTweener.Play(true);
buildPanelOpen = true;
}
if(Input.GetMouseButtonDown(0) && lastHitObj)
{
if(lastHitObj.tag == "PlacementPlane_Open")
{
GameObject newStructure = instantiane(allStructures[structureIndex], lasHitObj.transform.position, Quaternion.identity);
newStructure.transform.localEulerAngles.y = ( Random.Range ( 0, 360 ) );
lastHitObj.tag = "PlacementPlane_Taken";
}
}
}
void UpdateGUI () {
//Go through all structure buttons (buttons in the build panel). and set them to “off”
for(UISlicedSprite theBtnGraphic )//in buildBtnGraphics
{
theBtnGraphic.color = offColor;
}
//set the selected build button to “on”
buildBtnGraphics[structureIndex].color = onColor;
}
void SetBuildChoice ( GameObject btnObj )
{
string btnName = btnObj.name;
if ( btnName == "Btn_Canoon")
{
structureIndex = 0;
}
else if ( btnName == "Btn_Missle" )
{
structureIndex = 1;
}
UpdateGUI ();
}
}
C# has foreach loops, so you could do:
foreach (UISlicedSprite theBtnGraphic in buildBtnGraphics) {
theBtnGraphic.color = offColor;
}
or you can do a regular for loop:
for (int i = 0; i < buildBtnGraphics.Length; i++) {
buildBtnGraphics*.color = offColor;*
}
I made few correct but still there are three errors. I have no ide whats wrong… `using UnityEngine;
using System.Collections;
public class InGAmeGUI : MonoBehaviour {
//NGU items
public bool buildPanelOpen = false;
public TweenPosition BuildPanelTweener;
public TweenRotation BuildPanelArrowTweener;
//Placement Plane Items
public Transform placementPlanesRoot; // attach the placement plane root
public Material hoverMat; // attach the hover material to this slot
public LayerMask placementLayerMask; // calls up the layer mask selection
private Material originalMat;
private GameObject lastHitObj;
//Build Selection Items
public Color onColor;
public Color offColor;
public GameObject[] allStructures;
public UISlicedSprite[] buildBtnGraphics;
private int structureIndex = 0;
void Start ()
{
//reset the structure index, refresh the GUI
structureIndex = 0;
UpdateGUI();
}
void Update ()
{
if ( buildPanelOpen )
{
var ray = Camera.main.ScreenPointToRay ( Input.mousePosition );
RaycastHit hit;
if ( Physics.Raycast ( ray,out hit, 1000, placementLayerMask ) )
{
if ( lastHitObj )
{
lastHitObj.renderer.material = originalMat;
}
lastHitObj = hit.collider.gameObject;
originalMat = lastHitObj.renderer.material;
lastHitObj.renderer.material = hoverMat; // sets the planes material to the highlighted material
}
else // if the raycast didn’t hit anything
{
if ( lastHitObj ) // if we had previously hit something
{
lastHitObj.renderer.material = originalMat; // visually de select that object
lastHitObj = null; // nullify the plane selection
}
}
// drops a turrent on click
if ( Input.GetMouseButtonDown(0) && lastHitObj ) //left mouse button was clicked and there is a last hitObject // if left mouse button (0) is clicked and we have something selected
{
if(lastHitObj.tag =="PlacementPlane_Open")
{
// drop the chosen structure exactly at the tile’s position and rotation of Zero. See how the “index” comes into play here?
GameObject newStructure = 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)); //newStructure.transform.localEulerAngles.y = ( Random.Range ( 0, 360 ) );
// set this tile’s tag to “Taken” so we can’t double place a structure
lastHitObj.tag = "PlacementPlane_Taken";
}
}
}
}
void UpdateGUI () {
//Go through all structure buttons (buttons in the build panel). and set them to “off”
foreach(UISlicedSprite theBtnGraphic in buildBtnGraphics )//in buildBtnGraphics
{
theBtnGraphic.color = offColor;
}
//set the selected build button to “on”
buildBtnGraphics[structureIndex].color = onColor;
}
// this happens whenever the build arrow is clicked
void ToggleBuildPanel ()
{
if ( buildPanelOpen ) {
// hide all build tiles
foreach(Transform thePlane in placementPlanesRoot)
{
thePlane.gameObject.renderer.enabled =false;
}
// plays the build panel tweener script backward ( false )
buildPanelTweener.Play ( false );
// sets the boolean to false ( closed )
buildPanelOpen = false;
}
else // the build panel was closed, so instead do this…
{
BuildPanelTweener.Play(true);
BuildPanelArrowTweener.Play(true);
foreach ( Transform thePlane in placementPlanesRoot )
{
thePlane.gameObject.renderer.enabled = true;
}
}
}
// Called whenever a structure choice is clicked ( the button in the build panel )
void SetBuildChoice ( GameObject btnObj )
{
string btnName = btnObj.name;
if ( btnName == "Btn_Farmer")
{
structureIndex = 0;
}
else if ( btnName == "Btn_Cow" )
{
structureIndex = 1;
}
else if ( btnName == "Btn_Alien")
{
structureIndex = 2;
}
UpdateGUI ();
}
} `