Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by LT23Live · Feb 19, 2014 at 12:39 AM · guiinstantiateboolrectgui-button

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; 
             }
 }
 }
 
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LT23Live · Feb 19, 2014 at 01:56 AM 0
Share

Thanks I clicked The male button and the human button Heres the snapshot of the debug alt text

debug.png (53.4 kB)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Look for object, instantiate if not there, and finally add an impulse 1 Answer

GUI Button screen Width and Centering Problem 4 Answers

Js Making windows/groups buttons react to other groups? 0 Answers

Enable and disable a button selection 3 Answers

strange Rect behavior on Y axis 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges