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 Xyex Development · Mar 25, 2013 at 10:37 PM · cameraguilock

Solved One Problem, Got another

Okay So I was writing a script in which I was attempting to lock camera movement when a GUI pulls up. The problem I am having is that I have multiple GUI's that all need to affect camera movement, but when I apply the locking scripts to the script that displays the gui to multiple objects, only one out of the say, five objects actually works. The scripts don't break the gui's but they don't use the locking motion. Here's the two scripts below.

GUI Object 1: var information: String;

 var script: MouseLookPlus;
 
 var script2: MouseLookPlus;
 
 private var guiOn = false;
 
 private var rect: Rect;
 
 function Mouse1 () {
     
     script = GameObject.Find("Main Camera").GetComponent(MouseLookPlus);
     script.enabled = false;
     script2 = GameObject.Find("First Person Controller").GetComponent(MouseLookPlus);
     script2.enabled = false;
 
 }
 
 function Mouse11 () {
     
     script = GameObject.Find("Main Camera").GetComponent(MouseLookPlus);
     script.enabled = true;
     script2 = GameObject.Find("First Person Controller").GetComponent(MouseLookPlus);
     script2.enabled = true;
     
 }
 
 function OnMouseDown () {
     guiOn = true;
     rect = Rect(Screen.width/2-120,Screen.height/2-100,300,300);
 }
 
 
 function OnGUI () {
 
     if (guiOn){
     
         Mouse1();
         
         GUI.Label(rect, information);
         
         if (GUI.Button(Rect(Screen.width/2-0,Screen.height/2-50,75,50), "False")) {
                 guiOn = false;
         }
         if (GUI.Button(Rect(Screen.width/2-100,Screen.height/2-50,75,50), "True")) {
             animation.Play ("dooropen1");
             guiOn = false;
         }
         
     }
     
     else {
         Mouse11 ();
     
     }
     
 }

GUI Object 2:

 var information: String;
 
 var script: MouseLookPlus;
 
 var script2: MouseLookPlus;
 
 private var guiOn = false;
 
 private var rect: Rect;
 
 function Mouse2 () {
     
     script = GameObject.Find("Main Camera").GetComponent(MouseLookPlus);
     script.enabled = false;
     script2 = GameObject.Find("First Person Controller").GetComponent(MouseLookPlus);
     script2.enabled = false;
 
 }
 
 function Mouse22 () {
     
     script = GameObject.Find("Main Camera").GetComponent(MouseLookPlus);
     script.enabled = true;
     script2 = GameObject.Find("First Person Controller").GetComponent(MouseLookPlus);
     script2.enabled = true;
     
 }
 
 function OnMouseDown () {
     guiOn = true;
     rect = Rect(Screen.width/2-120,Screen.height/2-100,300,300);
 }
 
 
 function OnGUI () {
 
     if (guiOn){
     
         Mouse2();
         
         GUI.Label(rect, information);
         
         if (GUI.Button(Rect(Screen.width/2-0,Screen.height/2-50,75,50), "False")) {
                 guiOn = false;
         }
         if (GUI.Button(Rect(Screen.width/2-100,Screen.height/2-50,75,50), "True")) {
             animation.Play ("dooropen2");
             guiOn = false;
         }
         
     }
     
     else {
         Mouse22 ();
     
     }
     
 }

I'm not getting any error, and the code renders, just doesn't work properly, are the locking scripts possibly conflicting with each other? Is there a script I can tie to just the first person controller when a gui is on? Help would be appreciated.

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 iwaldrop · Mar 25, 2013 at 11:46 PM

You shouldn't be calling methods from OnGUI, but it doesn't look like it would cause the problem you're describing. Also, calling GetComponent() is expensive, and you should probably just cache references to them in Awake().

You might try pulling your method calls out of OnGUI to see if that fixes your problem, but otherwise I'm afraid I don't understand what you're asking. It isn't really clear what you're trying to do here. If you want to decide whether or not to turn the GUI on then you should be doing that in your OnMouseDown() function. That way you're not turning it on just to decide whether you should leave it on.

Also, there doesn't seem to be any difference in these scripts aside from "dooropen1" and "dooropen2". I'd suggest making that a public string variable, setting it there, and trashing one or more of your scripts that does the same thing. Google DRY code. ;)

If you have any questions about any of this, reply to this answer as and we'll work it out.

Comment
Add comment · 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

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

Is there a way to change the order of the "Layers" Tab? 0 Answers

Cameras Switch Based On Button Click 1 Answer

IS this possible to stay GUIs while translating camera 1 Answer

How to move GUI objects to separate Camera Layer - and why? 1 Answer

Apply Render Texture To GUI + Transparency? 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