- Home /
Start() is called on Play and on Stop
I have a script attached to a gameObject. Whenever I press play it Start() function gets called, but when pressing play again to stop play mode, it's called again. I use a custom inspector and a custom property drawer on it and one of its fields. The call stack just shows Start() being called by the engine and not any manual call from any script.
Is this common behaviour in some way, because I don't get it.
It's certainly not intended behaviour, and can't say I've ever seen it before. Bit hard to diagnose further without seeing your code.
Ok, that's basically the code. It must have something to do with deriving from the UI.Text component, but I can't see how:
using UnityEngine.UI;
using UnityEngine;
using UnityEditor;
[CanEdit$$anonymous$$ultipleObjects]
public class TestText : Text {
// // Use this for initialization
void Start () {
Debug.Log ("Start called");
}
}
Does it even matter if it's called on stopping? It's not normal but hey, you're stopping the program anyways, aren't you?
using UnityEngine.UI;
using UnityEngine;
using UnityEditor;
[CanEdit$$anonymous$$ultipleObjects]
public class TestText : Text
{
// // Use this for initialization
void Start()
{
if (Application.isPlaying) Debug.Log("Start called");
}
}
Answer by Bunny83 · Nov 05, 2016 at 06:07 PM
This makes no sense. "CanEditMultipleObjects" is an editor attribute an is only used on editor classes. Specifically on custom inspectors. The "UI.Text" class is a component and as such a runtime class.
Why do you actually want to derive your class from Text? In most cases you don't want to do that. You usually write component behaviour scripts which use UI components.
edit
btw: The reason why Start is called when you enter playmode and when you leave it is the ExecuteInEditMode attribute which is atttached to one of the base classes of Text. Specifically to the "Graphic" class. The hierarchy is:
Text -> MaskableGraphic -> Graphic -> UIBehaviour -> MonoBehaviour -> Behaviour -> Component
/|\
|
This has [ExecuteInEditMode]
For some reason I couldn't Reply to your comment so I converted it to an answer. yes, the CanEdit$$anonymous$$ultipleObjects was a mistake while writing that from scratch, I use it on the customeditor.
I wanted to derive from Text, because I need all the functionality except for access to the underlying m_Text variable.
I've added an explanation for your additional "Start" call.