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 gianni123 · Mar 06, 2012 at 04:03 PM · variabledisableenableanother

enable disable variable of a script from another script

how can I enable or disable a variable in a script attached to a gameobject from another script?

here is my code

 var mouseON=true;

var characterON=true;

function Update () {

GameObject.Find("Player").GetComponent("MouseLook").enabled=mouseON;

GameObject.Find("Player").GetComponent("CharacterMotor").enabled=characterON;

}

and to enable I use

 GameObject.Find("Player").GetComponent("EnablePlayer").enabled=true;

how can I can change the value of mouseON only?

i try

 GameObject.Find("Player").GetComponent("EnablePlayer").GetComponent("mouseON")=false;

but don't work

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Jacek_Wesolowski · Mar 06, 2012 at 04:27 PM

Create two fields: player_mouse_look and player_character_motor.

In the Start() function (not Update() ) do the following:

 player_mouse_look = GameObject.Find("Player").GetComponent("MouseLook");
 player_character_motor = GameObject.Find("Player").GetComponent("CharacterMotor");

These two lines mean that you look for the components you need once, and store the information on where they are so you don't have to look for them every frame. You won't need the code in the Update() function anymore. Instead, whenever you want to enable mouselook, just do this:

 player_mouse_look.enabled = true;

Same thing for player_character_motor.

You might want to check if the two variables you created are not null. It should only happen when a component is missing, though.

Also, if the components you use change during the game for any reason, you're going to need to update the variables whenever that happens.

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 gianni123 · Mar 06, 2012 at 04:40 PM 0
Share

thanks, but this was just an example, I would like to understand how I can turn on a variable of a script from another script.

avatar image
0

Answer by gianni123 · Mar 06, 2012 at 04:38 PM

thanks, but this was just an example, I would like to understand how I can turn on a variable of a script from another script.

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

Answer by worldofcars · Mar 06, 2012 at 04:40 PM

var Player : GameObject; //Drag your Player in here

function Update (){ }

function Start () { Player.GetComponent("MouseLook").enabled = true; }

Comment
Add comment · Show 5 · 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 gianni123 · Mar 06, 2012 at 05:10 PM 0
Share

your script give me this error: ArgumentException: find can only be called from the main thread

var Player = GameObject.Find("Player");

function Update (){ } function Start () {

Player.GetComponent("$$anonymous$$ouseLook").enabled = true;

} }

avatar image worldofcars · Mar 06, 2012 at 05:16 PM 0
Share

You get this error because you don't copy my script right. I wrote "var Player : GameObject;" and you wrote "var Player = GameObject.Find("Player")"... If you like, you can also use this script here:

function Update (){ }

function Start () {

var Player = GameObject.Find("Player");

Player.GetComponent("$$anonymous$$ouseLook").enabled = true;

}

avatar image gianni123 · Mar 06, 2012 at 05:50 PM 0
Share

I can not explain well. I changed the question.

avatar image gianni123 · Mar 06, 2012 at 05:52 PM 0
Share

how can I enable or disable a variable in a script attached to a gameobject from another script?

here is my code

 var mouseON=true;

var characterON=true;

function Update () {

GameObject.Find("Player").GetComponent("$$anonymous$$ouseLook").enabled=mouseON;

GameObject.Find("Player").GetComponent("Character$$anonymous$$otor").enabled=characterON;

}

and to enable I use

 GameObject.Find("Player").GetComponent("EnablePlayer").enabled=true;

how can I can change the value of mouseON only?

avatar image gianni123 · Mar 06, 2012 at 05:59 PM 0
Share

i try

GameObject.Find("Player").GetComponent("EnablePlayer").GetComponent("mouseON")=false;

don't work.

avatar image
0

Answer by worldofcars · Mar 06, 2012 at 10:16 PM

Maybe this is what you are looking for:

var mouseON : boolean = true;

var characterON : boolean = true;

function Start () { var Player : GameObject.Find("Player"); if(Player.GetComponent("MouseLook").enabled == true){ mouseON = false; } if(Player.GetComponent("EnablePlayer").enabled == true){ //charakterON = false; //or //charakterON = true; } }

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

6 People are following this question.

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

Related Questions

Script Not staying disabled 2 Answers

Turn Off/on culling mask by script? 2 Answers

Unable to enable script 0 Answers

disable/enable mouselook not working? 1 Answer

Enable/Disable GameObject Button script 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