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
0
Question by KingKongFu · Sep 27, 2012 at 02:55 PM · guieditorif

If statement is not working properly

So this is what I am trying to do. When you click the left, right or middle mouse button I want to do one of 3 things. 1 display the information off the gameobject from a script. 2 destroy the gameObject, 3 edit the information that is attached to the gameObject. So the first two are working flawlessly. but the 3rd on is not working at all it wont even display the debug log that I put in it. I am completly lost why this is not working. Both functions are set up the same and they should behave the same way but it does not. Any help with this would be appreciated. The script that is attached to the gameObject is just a bunch of strings and ints not methods just so I can assign values in the unity Gui editor and then adjust them during runtime with the script

 using UnityEngine;
 using System.Collections;
 
 public class GunTest : MonoBehaviour 
 
 {
     //the script called GeoData
     //it is attached to a cube that holds variables for that specific cube
     public GeoData geoData;
     //variable for accessing the information of the objects
     private string targetName = string.Empty;
     private string targetId;
     private string targetGeoId;
     private string targetHeight;
     private string targetBuildingType;
     private string targetCondition;
     private string targetHWT;
     private bool lookingAtObject;
     
     //variables for editing menu
     bool editObject;
     bool showEditGui;
     string editName;
     
     // the result object
     RaycastHit hitInfo;
      
     
     private void Update()
     {
         // get the center of the gun
         var origin = this.transform.position;
         // get the direction of the gun
         var direction = this.transform.forward;
         // get the distance to test for (how long can the gun shoot)
         var distance = 20.0f;
         // the result object
         //RaycastHit hitInfo;
 
         // check if the player is targeting anything
 
         if( !Physics.Raycast( origin, direction, out hitInfo, distance )  )
 
         {
             // nothing targeted
             this.targetName = string.Empty;
             this.targetId  = string.Empty;
             this.targetGeoId = string.Empty;
             this.targetHeight = string.Empty;
             this.targetBuildingType = string.Empty;
             this.targetCondition = string.Empty;
             this.targetHWT = string.Empty;
             lookingAtObject = false;
             editObject = false;
             return;
 
         }
             
         // store the name of the target
         this.targetName = hitInfo.collider.name;
         //get the variable from the script
         //assign geoId to local variable
         this.geoData = hitInfo.collider.GetComponent<GeoData>();
         int tId = geoData.id;
         //tId.ToString();
         this.targetId  = tId.ToString();
         this.targetGeoId = geoData.geoId;
         int tH = geoData.height;
         this.targetHeight = tH.ToString(); 
         this.targetBuildingType = geoData.buildingType;
         this.targetCondition = geoData.condition;
         this.targetHWT = geoData.hwt;
         
         
         //destroy an object your looking at left click
         if(Input.GetButtonDown ("Fire1")) 
         {
             lookingAtObject = true;
         }
         
         //edit object attributes right click
         if(Input.GetButtonDown("Fire2"))
         {
             editObject = true;
         }
         if(Input.GetButtonDown("Fire3"))
         {
             Destroy(hitInfo.collider.gameObject);
         }
             
     }
     
     //this is to display information from gameObject
     private void OnGUI()
 
     {
         // output the name of what is targeted
         if(lookingAtObject == true)
         {
                GUI.Box(new Rect(0,0,Screen.width/2,Screen.height/2), "GeoView");
             GUILayout.Label("" );
             GUILayout.Label("ID: " + this.targetId);
             GUILayout.Label("GeoID: " +this.targetGeoId);
             GUILayout.Label("Height: " + this.targetHeight);
             GUILayout.Label("Building Type: " + this.targetBuildingType);
             GUILayout.Label("Building Condition: " + this.targetCondition);
             GUILayout.Label("House of Worship: " +this.targetHWT);
         }
     }
     
     
     private void EditGUI()
     {
         Debug.Log("In Edit Gui");
         if(editObject == true)
         {
             // draw gui stuff
             Debug.Log("in If statement");
             GUI.Box(new Rect(0,0,Screen.width/2,Screen.height/2), "GeoEditor");
             //this.targetGeoId = GUI.TextField(new Rect(10, 10, 200, 20), editName, 25);
             if (GUI.Button (new Rect (10,30,150,100), "Finish"))
                 FinishedEditing();
         }
         
     }
 
     // assign the name and close the GUI
     void FinishedEditing()
     {
             this.targetGeoId = editName;
         editObject = false;
         // continue running the game and whatever else you need to do here
     }
 }
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
Best Answer

Answer by MarkFinn · Sep 27, 2012 at 05:15 PM

You have the boolean value editObject which is set on click and the method EditGUI(), but you never do anything with either of them.

The display data and destroy work just fine, but you never actually call the EditGUI method.

So, add

       if(editObject) EditGUI();

to your OnGUI method, and you should be fine.

Comment
Add comment · Show 2 · 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 ThunderAwesome · Sep 27, 2012 at 05:33 PM 0
Share

Beat me to it. You always want to make sure you are calling GUI-related functions inside OnGUI so that they can update correctly. Just adding my two-cents. Here's what I was gonna say:

From my understanding, EditGUI needs to be in the OnGUI function. There is no updating happening; therefore, nothing will display since EditGUI is not being called. As far as I know, EditGUI isn't a Unity function, it is your own function that needs to be passed into an update like OnGUI.

avatar image KingKongFu · Sep 27, 2012 at 05:36 PM 0
Share

AWWW thank you $$anonymous$$arkFinn and ThunderAwesome that got it working and now i get it. I just though that my own functions would update along with the onGUI, that clears things up a lot. Thanks again

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

11 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

Related Questions

uGUI - aligning to the bottom of another element 0 Answers

Wearied Error 1 Answer

How do I toggle the active state of a canvas UI object 0 Answers

Drawing Editor Inspector GUI based on selected/current prefab (CustomPropertyDrawer) 2 Answers

How do I prevent "Argument Exception: Getting control 1's position in a group with only 1 controls when doing repaint" in OnGUI function of my CustomPropertyDrawer? 2 Answers


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