- Home /
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
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.
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.
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.
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; }
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;
} }
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;
}
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?
i try
GameObject.Find("Player").GetComponent("EnablePlayer").GetComponent("mouseON")=false;
don't work.
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; } }
Your answer
Follow this Question
Related Questions
Script Not staying disabled 2 Answers
Turn Off/on culling mask by script? 2 Answers
Unable to enable script 0 Answers