- Home /
Using a GUI.Button to Instantiate gameObject using another gameObject's location
Hi again Unity Answers! This is my second question here.
I am finishing something for class. I am trying to get my GUI.Buttons to trigger a gameObject to Instantiate onto another gameObject's location.
I have set a couple bools to either be true or false.
ex. If Male bool and Human bool are both set to true, set all others to false and instantiate humanMale gameObject
It is not working :/ I am sure it is something simple but I would most definitely love somebody's help.
using UnityEngine; using System.Collections;
public class MainMenu : MonoBehaviour {
public Texture2D Next;
public Texture2D NextRollOver;
public string NextlevelToLoad;
public Texture2D Back;
public Texture2D BackRollover;
public string BacklevelToLoad;
//Race
private bool humanButton;
public Texture2D Human;
public Texture2D HumanRollOver;
private bool centaurButton;
public Texture2D Centaur;
public Texture2D CentaurRollOver;
private bool demonButton;
public Texture2D Demon;
public Texture2D DemonRollOver;
private bool maleButton;
public Texture2D Male;
public Texture2D MaleRollOver;
private bool femaleButton;
public Texture2D Female;
public Texture2D FemaleRollOver;
public GameObject spawn;
public GameObject humanMale;
public GameObject humanFemale;
public GameObject centaurMale;
public GameObject centaurFemale;
public GameObject demonMale;
public GameObject demonFemale;
private Rect WindowRect0 = new Rect( 270 , 80, 600 , 800);
void Start(){
OnGUI ();
humanButton = true;
centaurButton = false;
demonButton = false;
maleButton = true;
femaleButton = false;
raceNextButton = false;
}
void OnGUI(){
WindowRect0 = GUI.Window (0, WindowRect0, DoMyWindow, "Race");
}
void DoMyWindow (int windowID) {
if (windowID == 0) {
if (maleButton = GUI.Button (new Rect (90, 40, 100, 100), Male)) {
print ("Got a click 'Male' in window" + windowID);
if (maleButton == true) {
maleButton = false;
print ("Male = false");
}
if (maleButton == false) {
maleButton = true;
print ("Male = True");
}
}
if (femaleButton = GUI.Button (new Rect (390, 40, 100, 100), Female)) {
print ("Got a click 'Female' in window" + windowID);
if (femaleButton == true) {
femaleButton = false;
print ("Female = false");
}
if (femaleButton == false) {
femaleButton = true;
print ("Female = True");
}
}
if (humanButton = GUI.Button (new Rect (90, 150, 400, 110), Human)) {
print ("Got a click in window" + windowID);
if (humanButton == true) {
humanButton = false;
print ("human = false");
}
if (humanButton == false) {
humanButton = true;
print ("human = True");
}
}
if (centaurButton = GUI.Button (new Rect (90, 270, 400, 110), Centaur)) {
print ("Got a click in window" + windowID);
if (centaurButton == true) {
centaurButton = false;
print ("centar = false");
}
if (centaurButton == false) {
centaurButton = true;
print ("centaur = True");
}
}
if (demonButton = GUI.Button (new Rect (90, 390, 400, 110), Demon)) {
print ("Got a click in window" + windowID);
if (demonButton == true) {
demonButton = false;
print ("demon = false");
}
if (demonButton == false) {
demonButton = true;
print ("demon = True");
}
}
if (GUI.Button (new Rect (40, 650, 100, 100), Back)) {
print ("Got a click in window" + windowID);
Application.LoadLevel (BacklevelToLoad);
}
if (raceNextButton = GUI.Button (new Rect (440, 650, 100, 100), Next)) {
print ("Got a click in window" + windowID);
}
}
void OnMouseUp(){
if (maleButton == true) {
femaleButton = false;
}
if (femaleButton == true) {
maleButton = false;
}
if (humanButton == true) {
centaurButton = false;
demonButton = false;
}
if (centaurButton == true) {
humanButton = false;
demonButton = false;
}
if (demonButton == true) {
humanButton = false;
centaurButton = false;
}
if ((humanButton == true) & (maleButton == true)) {
Instantiate (humanMale, spawn.transform.position, Quaternion.identity);
raceNextButton = true;
}
if ((humanButton == true) & (femaleButton == true)) {
Instantiate (humanFemale, spawn.transform.position, Quaternion.identity);
raceNextButton = true;
}
if ((centaurButton == true) & (maleButton == true)) {
Instantiate (centaurMale, spawn.transform.position, Quaternion.identity);
raceNextButton = true;
}
if ((centaurButton == true) & (femaleButton == true)) {
Instantiate (centaurFemale, spawn.transform.position, Quaternion.identity);
raceNextButton = true;
}
if ((demonButton == true) & (maleButton == true)) {
Instantiate (demonMale, spawn.transform.position, Quaternion.identity);
raceNextButton = true;
}
if ((demonButton == true) & (femaleButton == true)) {
Instantiate (demonFemale, spawn.transform.position, Quaternion.identity);
raceNextButton = true;
}
}
}
Answer by iHaveReturnd · Feb 19, 2014 at 01:24 AM
Change your if statements that are nested to be else ifs. For example:
if (maleButton = GUI.Button (new Rect (90, 40, 100, 100), Male)) {
print ("Got a click 'Male' in window" + windowID);
if (maleButton == true) {
maleButton = false;
print ("Male = false");
}
else if (maleButton == false) {
maleButton = true;
print ("Male = True");
}
}
Right now if it's true, youre setting it false, then checking right away if its false, and setting it back to true in that instance. And most of your other ones do the same thing. To help us out you could mention which debugs and whatnot are passing, but I think the root of the problem will be that.
Thanks I clicked The male button and the human button Heres the snapshot of the debug