- Home /
How many times awake function is called when toggle on/off gameObjects with the same C# script
In my scene I toggle on/off characters that has the same script.
My question is: How is the script working in this case? I call the Awake() function when level starts, but how does it work on the disabled characters? will the Awake function be called once they are enabled?
Thanks, I know it sounds trivial...
Answer by Reynarz · Aug 16, 2017 at 06:22 PM
Did you saw this?https://docs.unity3d.com/Manual/ExecutionOrder.html
Awake is called Only if the gameObject is enabled and only one time.
If you want call a function every time when an object is enabled use:
private void OnEnable()
{
Debug.Log("Im enabled right now")
}
In this case, the avatar is toggled off, so the awake function is not called:
Awake: This function is always called before any Start functions and also just after a prefab is instantiated. (If a GameObject is inactive during start up Awake is not called until it is made active.)
but then... also onEnable is called only when the object is toggled on. If an object is toggled on and off, can the toggle on part treated as instantiating the object? if I enable an object than I instantiate it? or Instantiate mean writing a code line, with this method?
Thanks, I really want to know the difference before start changing the scripts.
Instantiate means: bring to the life an object that is not in Scene. That object is called Prefab Look this code Example:
/*This will be your prefab, just drag it from a folder of your project and drop it in this field, throught the inspector*/
public GameObject _myPrefab;
private void Start()
{
//This is called instantiation,(bring to the life an object that is not in Scene)
UnityEngine.Object.Instantiate(_myPrefab);
}
If that predab is not active (Or toggled on like you say) Nothing will be called until you SetActive(true) that prefab: Just get the GameObject instantiated
var myInstantiatedObject = UnityEngine.Object.Instantiate(_myPrefab);
myInstantiatedObject .SetActive(true);//Now all will be called, if the instantiated prefab was instantiated inactive of course
Look this video for more information : https://www.youtube.com/watch?v=4rZAAHevq1s
Hi @Reynardz,
In my scene, I have rooms, and the first room is enabled when scene is loaded, while the others are disabled, but the avatars inside these rooms are set to be enabled (but the room is off, so they are "off" as well). Once I enable the next room, and thus disable the previous, the avatars automatically "active" and execute their script.
So I don't use setActive, as they already are, and maybe I should?
$$anonymous$$y question then, does that mean, that once the room is enabled, then the awake function will be called? Or should I set them active by code once the room is enabled?
Yes, Awake will be called. SetActive() is for enable a disabled GameObject, then if the players are enabled you don't need enabled them again
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Script only works onetime 0 Answers
Use a toggle button to change a GameObjects visibility 2 Answers
Distribute terrain in zones 3 Answers
NullRefrenceExeption on Awake and in some other places 2 Answers