Hey i am making a script that are gonna spawn turrets on a plane in a tile system but i am having a problem actuly spawning the turret at a tile my problem is at GameObject newStructure = Instantiate(allStructures[structureIndex], lastHitObj.transform, Quaternion.identity);
I can´t see the problem so would be nice with some help
and here is my whole script
using UnityEngine;
using System.Collections;
public class InGameGUI: MonoBehaviour {
//NGUI items
public bool buildPanelOpen = false;
public TweenPosition buildPanelTweener;
public TweenRotation buildPanelArrowTweener;
//
//Placement Plane items
public Transform placementPlanesRoot;
public Material hoverMat;
public LayerMask placementLayerMask;
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) //if the build panel is open...
{
//create a ray, and shoot it from the mouse position, forward into the game
Ray ray= Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if(Physics.Raycast (ray, out hit, 1000f, placementLayerMask)) //if the RAY hits anything on right LAYER, within 1000 meters, save the hit item in variable "HIT", then...
{
if(lastHitObj) //if we had previously hit an object...
{
lastHitObj.renderer.material = originalMat; //visually de-select that object
}
lastHitObj = hit.collider.gameObject; //replace the "selected plane" with this new plane that the raycast just hit
originalMat = lastHitObj.renderer.material; //store the new plane's starting material, so we can reset it later
lastHitObj.renderer.material = hoverMat; //set the plane's material to the highlighted look
}
else //...if the raycast didn't hit anything (ie, the mouse moved outside the tiles) ...
{
if(lastHitObj) //if we had previously hit something...
{
lastHitObj.renderer.material = originalMat; //visually de-select that object
lastHitObj = null; //nullify the plane selection- this is so that we can't accidentally drop turrets without a proper and valid location selected
}
}
//drop turrets on click
if(Input.GetMouseButtonDown(0) lastHitObj) //left mouse was clicked, and we have a tile selected
{
if(lastHitObj.tag == "PlacementPlane_Open") //if the selected tile is "open"...
{
//drop the chosen structure exactly at the tile's position, and rotation of zero. See how the "index" comes in handy here? :)
GameObject newStructure = Instantiate(allStructures[structureIndex], lastHitObj.transform, Quaternion.identity);
//set the new structure to have a random rotation, just for looks
newStructure.transform.localEulerAngles.y = (Random.Range(0,360));
//set this tile's tag to "Taken", so we can't double-place structures
lastHitObj.tag = "PlacementPlane_Taken";
}
}
}
}
//
//-- Custom Functions --//
//One Update to Rule them All!
//this function will eventually contain all generic update events
//this makes sure we don't have the same small parts being called over and over in different ways, throughout the script
void UpdateGUI (){
//Go through all structure buttons (the buttons in the build panel), and set them to "off"
foreach(UISlicedSprite theBtnGraphic in buildBtnGraphics)
{
theBtnGraphic.color = offColor;
}
//set the selected build button to "on"
buildBtnGraphics[structureIndex].color = onColor;
}
//Called whenever a structure choice is clicked (the button in the build panel)
void SetBuildChoice ( GameObject btnObj ){
//when the buttons are clicked, they send along thier GameObject as a parameter, which is caught by "btnObj" above
//we then use that btnObj variable, and get it's name, so we can easily check exactly which button was pressed
string btnName = 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
void ToggleBuildPanel (){
if(buildPanelOpen) //is the build panel already open? if so, do this...
{
//hide all build tiles
foreach(Transform thePlane 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 do this...
{
//show all build tiles
foreach(Transform thePlane 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;
}
}
}