S
S
SkyCrusher2020-10-07 00:12:44
Unity
SkyCrusher, 2020-10-07 00:12:44

How to keep changing values ​​when switching to PlayMode?

Hello! In general, the situation is as follows: the components have methods available through the ContexMenu. They work as they should. But I decided to automate a little and write a script where these methods are called automatically. And here is where the problem lies. Component values ​​that are set during the execution of these methods are reset when PlayMode is started. How, exactly, to fix this?
Component Methods:
First:

[ContextMenu("Fill spaces")]
        public void FillSpaces()
        {
            connections = new List<Transform>();
            LineController[] lines = GameObject.FindObjectsOfType<LineController>();
            foreach (var line in lines)
            {
                string[] knotNames = line.gameObject.name.Split('-');
                if (knotNames.Length != 2)
                {
                    LoggerManager.Instance.Error(this, "Incorrect line name. Example: {KnotName1}-{KnotName2}");
                }
                if (knotNames[0] == gameObject.name)
                {
                    connections.Add(GameObject.Find(knotNames[1]).transform);
                }
                else if (knotNames[1] == gameObject.name)
                {
                    connections.Add(GameObject.Find(knotNames[0]).transform);
                }
            }
        }

Second:
[ContextMenu("Fill spaces")]
        public void FillSpaces()
        {
            List<string> knotNames = new List<string>(gameObject.name.Split('-'));
            top = GameObject.Find(knotNames[0]).GetComponent<Knot>();
            bottom = GameObject.Find(knotNames[1]).GetComponent<Knot>();
        }

The method that calls these methods:
[MenuItem("Config/AutoConfigLevel")]
        public static void AutoConfigLevel()
        {
            Knot[] knots = GameObject.FindObjectsOfType<Knot>();
            foreach (var knot in knots)
            {
                knot.FillSpaces();
            }
        
            LineController[] lines = GameObject.FindObjectsOfType<LineController>();
            foreach (var line in lines)
            {
                line.FillSpaces();
            }
        }

Visual difference after using it via ContexMenu and MenuItem:
ContexMenu:
5f7cdd493a2aa901791772.png
MenuItem:
5f7cdd23c5d87642914601.png
I suspect that PrefabUtility should be used, but I can't find something suitable there.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question