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 Willga321 · Feb 25, 2014 at 08:55 PM · variablecomponenttags

Access the same component

Hi, I have 2 scripts that have different functions but both have something in common that they do, when clicked on the both access the First Person Controller and disable the mouse look scripts using this code teleport.GetComponent("MouseLook").enabled = false; and both have the variable teleport defined in them using this code var teleport : GameObject; however when both these scripts are applied only one works, but when removed the other works again. I am very new to unity and JavaScript but I think it has something to do with the variables being defined in both scripts. Here is the rest of my code for one of the scripts if that is needed.

 var teleport : GameObject;
 
 var ydisable : GameObject;
 
 public var showstart : boolean = false; 
 
 public var showMessage : boolean = false; 
 var dialog : String = ""; 
 
 public var showMessage2 : boolean = false; 
 var dialog2 : String = ""; 
 
 public var showMessage3 : boolean = false; 
 var dialog3 : String = ""; 
 
 public var showMessage4 : boolean = false; 
 var dialog4 : String = "";
 
 public var showMessage5 : boolean = false; 
 var dialog5 : String = ""; 
 
 function Start () { 
 
 } 
 
 
 function Update () { } 
 
 function OnGUI() { 
 
     if(showstart == true) {
         
         teleport.GetComponent("MouseLook").enabled = false;
         ydisable.GetComponent("MouseLook").enabled = false;
     
         if(showMessage == true) {
     
             if(GUI.Button (Rect((Screen.width/4)-100, 
             (Screen.height/4)-100, 600,300), dialog)) {
     
                 showMessage = false;
                 showMessage2 = true;
 
 }
 }
 
         if(showMessage2 == true){
     
             if(GUI.Button (Rect((Screen.width/4)-100, 
             (Screen.height/4)-100, 600,300), dialog2)) {
         
                 showMessage2 = false;
                 showMessage3 = true;
  
 }
 
 }
 
         if(showMessage3 == true){
     
             if(GUI.Button (Rect((Screen.width/4)-100, 
             (Screen.height/4)-100, 600,300), dialog3)) {
         
                 showMessage3 = false;
                 showMessage4 = true; 
 }
 
 }
 
         if(showMessage4 == true){
     
             if(GUI.Button (Rect((Screen.width/4)-100, 
             (Screen.height/4)-100, 600,300), dialog4)) {
 
                 showMessage4 = false;
                 showMessage5 = true; 
 }
 
 }
 
         if(showMessage5 == true){
     
             if(GUI.Button (Rect((Screen.width/-4)- -1500, 
             (Screen.height/4)- -100, 600,300), dialog5)) {
         
                 showMessage5 = false;
                 showstart = false;
 }    
 
 }
 
 }
     if(showstart == false){
     
         teleport.GetComponent("MouseLook").enabled = true;
         ydisable.GetComponent("MouseLook").enabled = true;
     }
 }
 function OnMouseDown () {
     showstart = true;
     showMessage = true;
 
 }

Thanks for any help!

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 robertbu · Feb 25, 2014 at 09:12 PM

The issue is the order of execution of the scripts combined with the fact you are setting the 'enabled' flag on every frame. Actually since OnGUI() gets called multiple times per frame, you are setting the variable multiple times per frame.

I don't know which order the two scripts will have their OnGUI() called in, but let's assume that the above script gets called last. If the other script tried to change the 'enabled' state, your script above would overwrite the setting either on line 95 or on line 33.

Viewing the code out of context it is hard to give a firm recommendation for a 'best' fix, but a brute force solution is to do the enabling inside each GUI.Button() test. Anytime you potentially assign a new value to 'showstart' or 'showMessage', you would make the calls to enable/disable the appropriate 'MouseLook' script. That way, the 'enabled' flag would only be set when a button was clicked, not every frame.

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 Willga321 · Feb 26, 2014 at 02:36 PM 0
Share

Thanks for the answer, I think I understand why that fix would work but how exactly would I preform it? I've been using unity and javascript for about 3 weeks so I'm very much a newbie lol.

Edit: Never $$anonymous$$d, I read through your answer a few times and I figured it out! Thanks for your help!

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

20 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 avatar image

Related Questions

how do I find components through variable 1 Answer

Updating variable on another script. It is not working. 2 Answers

GetComponent in another script help 1 Answer

How to compare variable component types between GameObjects at run time? 1 Answer

Accessing components via script and assigning randomized values to their parameters? 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