- Home /
set variable when active
I have a model that is set to be active with SetActiveRecursively(true);
in the first script. As soon as this model is active I want a different script to set a variable "three" to 0. After that the variable has to be able to be changed by other parts of the script (if I put it in the update function it makes the variable always equal to 0). That way I can make it equal to 1 in another part of the script. Is there any way to do this?
The object with the first script is the camera and the second script is on the model(child object of camera). Thank you for helping me, this worked very well! I put this in the second script to reset the variable three to 0 when the object is active. Thanks for the help!
I converted my comment to a answer. If this answered your question, click on the checkmark to close it out.
Answer by robertbu · Mar 30, 2013 at 04:46 AM
Your question is a bit confusing...mostly because of "I want a different script to set a variable 'three' to 0." Does it need to be a different script? If it can be done in the same script you can use MonoBehaviour.OnEnable.
function OnEnable () {
three = 0;
Debug.Log("This script was just enabled");
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613095339im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Using a script as a member variable in another script. 2 Answers
Accessing A Variable From Another Script 4 Answers
How do I assign script as a variable in the inspector? 1 Answer
Editing a variable from another script on collision 3 Answers
How to set a variable equal to another variable in another gameobject? 2 Answers