EDIT: (Because it wasn’t too clear, and updated script)
My script still isn’t working correctly. I tried to instead of raycasting use mouseDown to try and localise it to the object. But because they’re both running the same script i’m getting undesirable results.
Here’s my ‘project’ (blank scene, and a default skybox) if someone would rather look at it directly than through my code. It was done in the latest version of unity for windows
I can get it to build a tower on a zone, but then the next time it’s clicked it’s supposed to show ‘destroy menu’ , however it gets confused between the two objects and gives strange results.
Help would be very much apprechiated
Here’s the script:
static Rect myRect;
static Vector2 mousePos;
public GameObject zoneTower;
string zoneTowerName;
int zoneTowerStength;
int zoneTowerMinHealth;
int zoneTowerMaxHealth;
int zoneTowerCurrHealth;
public static bool showDetailsGUI = false;
public static bool showBuildMenu = false;
public static bool showDestroyMenu = false;
static Vector3 dir;
canon canonInstance;
buildCube cubeInstance;
public delegate void upgrade ();
public static event upgrade UpgradeTower;
Vector3 OnMouseDown(){
print(this.gameObject.collider.name);
if (!zoneTower) {
if (Input.GetButtonDown ("Fire1")) {
mousePos = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
myRect = new Rect (mousePos.x, mousePos.y, 120, 110);
showBuildMenu = true;
}
} else {
if (Input.GetButtonDown ("Fire1")) {
mousePos = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
myRect = new Rect (mousePos.x, mousePos.y, 120, 110);
showDestroyMenu = true;
}
}
dir = transform.position;
return dir;
}
void OnGUI(){
if (showBuildMenu == true) {
showDestroyMenu = false;
myRect = GUI.Window (0, myRect, buildWindow, "Build Window");
}
if (showDestroyMenu == true) {
showBuildMenu = false;
myRect = GUI.Window (1, myRect, destroyWindow, "Destroy Window");
}
} //End OnGUI
void buildCanon(Vector3 dir){
zoneTower = canon.GetZoneTowerObj();
zoneTowerName = canon.towerName;
zoneTowerStength = canon.strength;
zoneTowerMinHealth = canon.minHealth;
zoneTowerMaxHealth = canon.maxHealth;
zoneTowerCurrHealth = canon.currHealth;
Vector3 newDir = new Vector3(dir.x, 0.09f, dir.z);
zoneTower = Instantiate (zoneTower, newDir, Quaternion.identity) as GameObject;
zoneTower.name = zoneTowerName;
showBuildMenu = false;
}
void buildCubes(){
zoneTower = buildCube.GetZoneTowerObj ();
zoneTowerName = buildCube.towerName;
zoneTowerStength = buildCube.strength;
zoneTowerMinHealth = buildCube.minHealth;
zoneTowerMaxHealth = buildCube.maxHealth;
zoneTowerCurrHealth = buildCube.currHealth;
Vector3 newDir = new Vector3(dir.x, 0.09f, dir.z);
zoneTower = Instantiate (zoneTower, newDir, Quaternion.identity) as GameObject;
zoneTower.name = zoneTowerName;
showBuildMenu = false;
}
void buildWindow(int windowID) {
if (GUI.Button (new Rect (10, 20, 100, 20), "Build Canon")) {
buildCanon (dir);
}
if (GUI.Button (new Rect (10, 50, 100, 20), "Build Cube")) {
buildCubes();
}
}//end domywindow
void destroyWindow(int windowID) {
if (GUI.Button (new Rect (10, 20, 100, 20), "Remove Tower")) {
Destroy(zoneTower);
showBuildMenu = false;
showDestroyMenu = false;
}
if (GUI.Button (new Rect (10, 50, 100, 20), "Upgrade")) {
UpgradeTower();
showBuildMenu = false;
showBuildMenu = false;
}
if (GUI.Button (new Rect (10, 80, 100, 20), "Return")) {
showBuildMenu = false;
showDestroyMenu = false;
}
}
}